to_json

Table of contents

Turns input into JSON, when possible. If input cannot be turned into JSON object, the filter just returns the input again.

{{ site | to_json }}
Input
{"id": 783, "content_type": { "name": "site", "title": "Site" }, "name": "A beautiful Site", ...}
Output
# params = { 'param_key_1' => 'param_val_1', 'param_key_2' => 'param_val_2' }
{{ params | to_json }}
Input
[["param_key_1", "param_val_1"], ["param_key_2", "param_val_2"]]
Output

When using to_json on a hash, a multidimensional array is returned (see above). If you want to convert a hash to key/value JSON, you can pass true as an argument. Plate Objects get converted to key/value JSON regardless of the passed argument.

# params = { 'param_key_1' => 'param_val_1', 'param_key_2' => 'param_val_2' }
{{ params | to_json: true }}
Input
{"param_key_1": "param_val_1", "param_key_2": "param_val_2"}
Output

Need help?

Do you have any question which is not answered in this knowledge base? Contact us. We are here to help you.