What Unescaping a JSON String Means
Unescaping is the inverse of escaping — it converts JSON escape sequences (like \n, \", \uXXXX) back into the raw characters they represent. If you have a JSON string that shows literal \n as two visible characters instead of an actual line break, unescaping fixes it.
The most common scenario: you copied a value out of a JSON API response, log line, or debug dump, and the escape sequences are showing as text instead of being processed. Unescaping recovers the original human-readable content so you can read it, save it to a file, or use it in code.
How Unescaping Works
The JSON spec defines a fixed set of escape sequences. An unescaper walks the input character by character, and whenever it encounters a backslash, reads the next character (or four hex digits for \uXXXX) to decide which raw character to output. \n becomes a newline (0x0A), \t becomes a tab (0x09), \" becomes a literal quote, \\ becomes a single backslash.
The safest way to unescape in code is to wrap the escaped content in double quotes and pass it to JSON.parse (in JavaScript) or json.loads (in Python). Both are heavily-tested parsers that handle every edge case correctly — surrogate pairs, invalid escapes, unicode boundaries. Rolling your own unescaper is a recipe for subtle bugs.
Unescaping in Different Languages
JavaScript
const escaped = 'Line 1\\nLine 2\\t\\"tabbed\\"';
// Wrap in quotes to make it a valid JSON string literal
const raw = JSON.parse('"' + escaped + '"');
console.log(raw);
// Line 1
// Line 2 "tabbed"
// If your escaped string may contain unescaped double quotes (unusual
// but possible in malformed input), pre-escape them first:
const safe = escaped.replace(/(?<!\\)"/g, '\\"');
const raw2 = JSON.parse('"' + safe + '"');Python
import json
escaped = 'Line 1\\nLine 2\\t\\"tabbed\\"'
# Wrap in double quotes to make it a valid JSON string literal
raw = json.loads('"' + escaped + '"')
print(raw)
# Line 1
# Line 2 "tabbed"
# Alternative: use .encode().decode('unicode_escape') — handles \n, \t,
# \uXXXX but NOT JSON-specific escapes like \/ and does not validate
raw2 = escaped.encode().decode('unicode_escape')Command Line (jq)
# echo the escaped string as JSON with jq, then extract the raw value
echo '"Line 1\nLine 2\t\"tabbed\""' | jq -r .
# jq -r outputs the raw string (no surrounding quotes, escapes decoded)
# If your escaped content is stored in a file:
cat escaped.txt | jq -Rr @json | jq -r .
# The -R reads raw lines, @json wraps them as JSON strings,
# and the second jq -r decodes back to rawCommon Escape Sequences and Their Meanings
The full set of JSON escape sequences: \" = double quote, \\ = backslash, \/ = forward slash (optional), \b = backspace (U+0008), \f = form feed (U+000C), \n = newline (U+000A), \r = carriage return (U+000D), \t = tab (U+0009), \uXXXX = any unicode character where XXXX is a four-hex-digit code point.
Unicode escapes above U+FFFF use a surrogate pair — two consecutive \uXXXX sequences encoding a high and low surrogate. For example the pile-of-poo emoji (U+1F4A9) is written as \uD83D\uDCA9. Any competent JSON parser handles this correctly; hand-rolled unescapers often do not.
When Unescaping Fails
1. Invalid escape sequences. If the input contains \z or another undefined escape, strict parsers throw an error. JavaScript's JSON.parse throws SyntaxError: Bad escaped character. If your source data has invalid escapes, either sanitize the input first or use a more lenient parser like Python's codecs.decode(s, 'unicode_escape').
2. Incomplete surrogate pairs. A lone \uD83D without its partner \uDCA9 is malformed unicode. Parsers may either replace with U+FFFD (replacement character) or throw. If you see ? characters in the output, this is likely the cause.
3. Already unescaped input. Feeding raw text (no escape sequences) through an unescaper does nothing harmful — but may indicate the caller was confused about the input format. Verify the source is actually escaped before unescaping.
Related JSON Tools
- JSON Formatter (Parent Tool) — the underlying formatter used by every variant
- Format JSON Online — general-purpose beautifier with 2/4-space and tab options
- Minify JSON Online — strip whitespace for production payloads
- JSON Validator — validate without formatting
- JSON Diff Checker — compare two JSON objects side-by-side