Skip to main content
All CollectionsBuild AutomationsWorkflowsData
Escape Special Characters in JSON Objects
Escape Special Characters in JSON Objects

Learn how to properly escape special characters and white space in JSON objects to successfully pass data between steps in Torq.

Updated over a week ago

During your workflow, you may want to turn data that you have received from context or outside sources into a JSON object. JSON objects allow for easy reference and manipulation. Still, if data is not already formatted for a JSON, it may include whitespace and other special characters that do not easily convert into JSON format. These characters must be escaped (modified into a string) to format the data in JSON and retain its integrity for future manipulation and reference.

Use Output Utilities to Convert Raw Data to JSON

In the following example, the data received in the Output Utility step named "raw data" contains several newline characters and quotation marks, both capable of breaking JSON objects.


An attempt to convert the given data to JSON format by the Add to JSON step will fail due to invalid characters.

Add the JSONEscape inline function to the data in the Add to JSON step, to escape the special characters in the raw data. Apply the inline function directly to the data like so: {"data":{{ jsonescape $.raw_data.output }}}

The result will be a JSON object with a single key:value pair. 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 passed through an HTTP request.

Did this answer your question?