JSON encode and decode
Converting to JSON
Here is an example of using the |json_encode filter to convert native data types to JSON strings.
-
start: set: data: name: Joe Customer order_id@int: 54321 status: text: shipped tracking_id: Z1F238 return: encoded: {{data|json_encode}} pretty: {{data|json_encode|json_pretty}}
-
__return: encoded: '{"name":"Joe Customer","order_id":54321,"status":{"text":"shipped","tracking_id":"Z1F238"}}' pretty: |- { "name": "Joe Customer", "order_id": 54321, "status": { "text": "shipped", "tracking_id": "Z1F238" } }
Converting from JSON using @json
The @json annotation converts a JSON string back into native types.
-
start: set: json_string@text: {"name":"Joe Customer","order_id":12345} decoded_data@json: {{json_string}} return: customer: {{decoded_data.name}} order_num@int: {{decoded_data.order_id}}
-
__return: customer: Joe Customer order_num: 12345
Converting from JSON using json_decode
Here is an example of using the json_decode() function to convert JSON strings back into native data types.
-
start: set: json_string@text: "{\"name\":\"Joe Customer\",\"order_id\":12345}" decoded_data@json: {{json_decode(json_string)}} return: customer: {{decoded_data.name}} order_num@int: {{decoded_data.order_id}}
-
__return: customer: Joe Customer order_num: 12345