Skip to content Skip to footer navigation
Sitelet https://statamic.dev/modifiers/to_json

To Json Modifier

Converts any variable into JSON.

stats:
- player: Luke Skywalker
score: 750
- player: Wedge Antilles
score: 688
- player: Jar Jar Binks
score: 1425
{{ stats | to_json }}
{!! Statamic::modify($stats)->toJson() !!}
[
{"player":"Luke Skywalker","score":750},
{"player":"Wedge Antilles","score":688},
{"player":"Jar Jar Binks","score":1425}
]

Options#

Pass pretty to format the output with line breaks and indentation.

Pass safe when outputting JSON inside HTML, like a <script> tag. It escapes characters such as < and " so your content can't break out of the markup.

You can combine both:

{{ stats | to_json:pretty:safe }}
{!! Statamic::modify($stats)->toJson(['pretty', 'safe']) !!}