Skip to content
ToolkitHub

JSON Escaper / Unescaper

Escape and unescape JSON strings

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 \u escapes 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.stringify on 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 \x41 or \' 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.

Frequently asked questions

Why does the escaped output start and end with quotes?

The output is a complete JSON string literal, and string literals are wrapped in double quotes. Remove them only if the destination adds its own quotes.

Why does Unescape say my input is invalid?

The input contains an escape sequence JSON does not support, such as \x41, or an unescaped double quote inside the string. Fix those characters and try again.

My log shows JSON with backslashes everywhere. What happened?

The JSON was stringified more than once. Unescape it, and repeat if the result is still an escaped string.

Does escaping JSON protect against SQL injection or XSS?

No. JSON escaping only makes text safe inside a JSON string. Use parameterized queries for SQL and HTML encoding or framework escaping for web pages.