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.
anyno argumentsstringanystringstringSerializes 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"}Renders a nil value as the JSON null literal.
Given
{"missing": nil}Template
{{ missing | json }}Result
nullSerializes a list, preserving its order in a JSON array.
Given
{"tags": ["go", "rust"]}Template
{{ tags | json }}Result
["go","rust"]Wraps a bare string in JSON quotes.
Given
{"name": "Alice"}Template
{{ name | json }}Result
"Alice"Selects indented output with the literal 'pretty' mode, using two spaces per level.
Given
{"user": {"name": "Alice"}}Template
{{ user | json:'pretty' }}Result
{
"name": "Alice"
}Shows that any mode other than 'pretty' falls back to compact output.
Given
{"scores": [1, 2]}Template
{{ scores | json:'inline' }}Result
[1,2]Indents a JSON array, placing one element per line.
Given
{"scores": [1, 2]}Template
{{ scores | json:'pretty' }}Result
[
1,
2
]