Get a Demo
Table of contents

json_encode Twig Filter

The json_encode filter encodes the value as a JSON compatible value.

{{ data|json_encode }}

If the value is a string then it'll be wrapped in double-quotes. If it's a number or boolean then no change is made. If it's an array or object then it'll be converted to the JSON equivalent. 

Assigning values Link

You can use the filter when assigning a value to a variable.

{% set var = data|json_encode %}
Get Started With Aptuitiv