String Utilities · 4 min read
How to Escape and Unescape JSON Strings
Embedding raw text inside a JSON string field is tricky — a single unescaped quote breaks the whole payload. A JSON escape tool handles the conversion in both directions and detects which one you need automatically.
1. Why JSON Needs Escaping
JSON uses a few characters as syntax. If those characters appear inside a string value, the parser thinks the string ended:
- Double quote
". The string delimiter. A quote inside a value must become\". - Backslash
\. The escape introducer. A literal backslash becomes\\. - Newline, tab, carriage return. Raw control characters. Become
\n,\t,\r. - Control characters below 0x20. Must be encoded as
\uXXXX— most editors hide these but they will crash a strict parser.
2. How Escaping Works
A correct escape walks the input character by character and rewrites the special ones:
- Quote
"→\" - Backslash
\→\\ - Newline →
\n - Tab →
\t - Carriage return →
\r - Backspace →
\b - Form feed →
\f - Forward slash
/→ optional, often left raw. JSON permits/unescaped.
Most languages and tools do this for you — but when building payloads by hand or concatenating strings, you need to do it yourself.
3. The Reverse: Unescape
Unescape is the inverse — useful when you have a string that is already escaped and you want the readable form:
- Reading JSON inside JSON. If a payload has been double-encoded (escaped inside a wrapper), unescape peels one layer off.
- Debugging LLM output. Some language models return JSON strings that have been escaped again by mistake. Unescape recovers the original text.
- Inspecting log lines. Server logs often show the escaped form because that is what was stored. Unescape reveals the actual character.
4. Unicode Handling
A robust escape tool gives you a choice on Unicode:
- Pass-through (recommended). Keep
中文,é,🚀as-is. Reads cleanly, smaller output. - Strict ASCII. Encode every non-ASCII character as
\uXXXX(BMP) or surrogate pairs (supplementary planes). Required by some older parsers and log systems. - Read the spec. JSON allows raw UTF-8 inside strings. Escaping to
\uXXXXis optional, not required. Most modern parsers handle raw UTF-8 without issue.
Try the JSON Escape Tool
Paste escaped or unescaped text — the tool detects the direction and converts in real time. Unicode-safe, browser-based, free.
Open Escape Tool →Best Practices for JSON String Escaping
- Let your library escape for you. In every major language,
JSON.stringifyand friends handle escaping automatically. Hand-built strings are the main source of bugs. - Never double-escape. If you receive an already-escaped string and escape it again, you get
\\ninstead of a real newline. Use the unescape tool to recover. - Watch for HTML in JSON. When JSON is embedded inside HTML, both layers need escaping. Encode for HTML first, then for JSON, or use safe APIs that handle both.
- Validate after manual escaping. Run the result through a JSON validator — if it parses without error, your escaping is correct.
- Use
JSON.stringify(obj)for single strings. To safely embed a string, wrap it in a one-key object, stringify it, then strip the wrapper quotes. This is more reliable than hand-escaping.
Frequently Asked Questions
What does JSON escape do?
JSON escape converts special characters — double quotes, backslashes, newlines, tabs, and control characters — into their escaped forms (\", \\, \n, \t). The result is a string that can safely sit inside a JSON value.
What does JSON unescape do?
JSON unescape is the reverse. It converts escaped sequences back to their raw characters, turning a string like \n into an actual newline. Useful for reading data that has been double-encoded by accident.
When do I need to escape JSON?
Whenever you are placing raw text inside a JSON string field and the text contains quotes, backslashes, or newlines. Common cases: embedding HTML or code snippets, serializing user input, and building API payloads from concatenated strings.
Can the tool auto-detect which direction to use?
Yes. A good tool inspects the input and picks escape or unescape automatically. If the input contains \n, \t, or \" sequences, it unescapes; otherwise it escapes. You can also toggle the direction manually.
Does JSON escape handle Unicode characters?
Yes. A correct tool preserves Unicode characters like 中文, é, and 🚀 unchanged by default. It can also encode them as \uXXXX escapes when you need strict ASCII output for older systems or log files.
Looking for more guides? See the full JSONXX How To index.