Guide
Escaping JSON for strings, code and nested payloads
By Logappradeep · ServiceNow developer
Sometimes JSON has to live inside a string: a webhook body stored as text in another JSON document, a payload in an environment variable, a test fixture hard-coded in source, or a message in a queue that only accepts string values. To do that safely, the characters that would end the string early or break the syntax must be escaped. Unescaping reverses the process, which is what you need when logs show JSON that has been stringified once (or twice) too many times.
What escaping changes
Escaping turns your input into a JSON string literal. The result is wrapped in double quotes, and inside it:
"becomes\"\becomes\\- line breaks become
\n, carriage returns\r, and tabs\t - other control characters become
\uescapes such as\u0001
So the input {"name": "Ada"} becomes "{\"name\": \"Ada\"}", ready to be dropped into another JSON value or a string in JavaScript, Java, C#, or Go source. Non-ASCII characters like é are left as they are, which is valid because JSON text is UTF-8. A forward slash is also left alone; escaping it as \/ is permitted but optional.
Unescaping
Unescape reads a JSON string literal and removes one layer of escaping. The surrounding double quotes are optional: if you paste escaped content such as {\"a\":1} without them, they are added for you. If the decoded text is itself valid JSON, for instance an object, the tool shows it pretty-printed so you can read it immediately. If your log line was encoded twice, you will see another escaped string; run Unescape again on that output.
Common mistakes
- Double escaping. Calling
JSON.stringifyon a value that is already a JSON string produces"{\"a\":1}"instead of an object. If consumers receive strings where they expect objects, look for an extra stringify step. - Escapes from other languages. Sequences such as
\x41or\'are valid in JavaScript or Python strings but not in JSON, so Unescape reports an error. Replace them before unescaping. - Using the wrong escaping for the context. JSON escaping does not make text safe for SQL, HTML, shell commands, or URLs. Each context has its own rules: parameterized queries for SQL, HTML entity encoding for markup, and percent-encoding for URLs.
- Shell quoting on top. When putting escaped JSON into a shell command, the shell may interpret backslashes again. Single-quoting the whole value in Bash avoids most surprises.
Escaping and unescaping run entirely in your browser, so payloads containing tokens or customer data are never uploaded.