If you need to escape illegal JSON characters (such as backslashes, double quotes, etc.) in your input parameters using Zapier, you can utilize the "Code by Zapier" action with the following JavaScript code. This script will process all your input variables, escape the illegal characters, and return the modified parameters as output. Here's how you can do it:
The only thing you need to do is define your input parameter NAME(S) and VALUE(S) and copy the code!
-
Escape Function: The escapeJsonString function is designed to replace illegal JSON characters with their corresponding escaped versions. This ensures that the strings are safe to be used in JSON format.
-
Iterating Through Inputs: The script iterates through each input parameter provided to the action. It checks if the parameter is a string and applies the escape function. If the parameter is not a string, it remains unchanged.
-
Output: The escaped or unchanged parameters are collected in an object (escapedOutput) and returned as the output of the action.
Copy this in the code section:
function escapeJsonString(str) {
return str.replace(/[\\]/g, '\\\\')
.replace(/[\"]/g, '\\"')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t');
}
var escapedOutput = {};
// Iterate over each input parameter
for (var key in inputData) {
if (inputData.hasOwnProperty(key)) {
var value = inputData[key];
if (typeof value === 'string') {
escapedOutput[key] = escapeJsonString(value);
} else {
escapedOutput[key] = value;
}
}
}
output = escapedOutput;
Prinstscreen from a zap example:

And the escaped result:
