- 28 Nov 2022
- 1 Minute to read
- DarkLight
Use JSON objects
- Updated on 28 Nov 2022
- 1 Minute to read
- DarkLight
Escape special characters in JSON objects
At any point during your workflow run, you may want to turn data in context into a JSON object for easy reference and manipulation. The data may include whitespaces and other special characters that will not be properly interpreted when converted to JSON format. These whitespaces and special characters need to be escaped to keep the data in JSON format.
In the following example, the data contains several newline characters and quotation marks, both of which break JSON objects.
An attempt to convert the data to JSON format will fail:
Use the jsonEscape inline function to escape the special characters in the raw data.
Apply the inline function directly to the data:{{ jsonescape $.raw_data.output }}
A JSON object consisting of a single key:value pair is created. The key is data and the value is "\nan \nexample\n". The special characters (newline and quotation marks) are kept and the original string can be easily restored when passing through an HTTP request.