json

Serializes a value to a compact JSON string.

Object keys always come out sorted alphabetically, so the output is stable regardless of the input map's insertion order. Pass 'pretty' to indent the output instead.

InputArgumentsReturnsanyno argumentsstringanystringstring
Source
BasicSource

Serializes a value to compact JSON with keys sorted alphabetically, so the output stays stable regardless of the input map's insertion order.

Given

{"user": {"role": "admin", "name": "Alice"}}

Template

{{ user | json }}

Result

{"name":"Alice","role":"admin"}
NullSource

Renders a nil value as the JSON null literal.

Given

{"missing": nil}

Template

{{ missing | json }}

Result

null
SliceSource

Serializes a list, preserving its order in a JSON array.

Given

{"tags": ["go", "rust"]}

Template

{{ tags | json }}

Result

["go","rust"]
StringSource

Wraps a bare string in JSON quotes.

Given

{"name": "Alice"}

Template

{{ name | json }}

Result

"Alice"
BasicSource

Selects indented output with the literal 'pretty' mode, using two spaces per level.

Given

{"user": {"name": "Alice"}}

Template

{{ user | json:'pretty' }}

Result

{
  "name": "Alice"
}
Compact fallbackSource

Shows that any mode other than 'pretty' falls back to compact output.

Given

{"scores": [1, 2]}

Template

{{ scores | json:'inline' }}

Result

[1,2]
SliceSource

Indents a JSON array, placing one element per line.

Given

{"scores": [1, 2]}

Template

{{ scores | json:'pretty' }}

Result

[
  1,
  2
]