How to Escape and Unescape JSON Strings (Quotes, Backslashes, Newlines & Unicode)
Unescaped quotes, backslashes and control characters can make JSON strings invalid and cause parsing errors. Here is exactly what JSON escaping is, the characters you must escape, real examples, and one-line code in JavaScript, Python, Java and C#.
What does escaping a JSON string mean?
A JSON string is wrapped in double quotes. Any character that would confuse the parser must be replaced with a backslash escape sequence. Escaping does not change the meaning of your text — it simply makes the string valid JSON so parsers can read it reliably. Unescaping is the reverse: turning those sequences back into readable characters, which is handy when you copy a value out of logs or an API response.
The characters you must escape
Inside a JSON string, escape double quotes, backslashes and every control character from U+0000 through U+001F. The table shows the common short escapes; other control characters use \u00XX. See RFC 8259, section 7.
| Character | Escaped as |
|---|---|
Double quote " | \" |
Backslash \ | \\ |
| Newline | \n |
| Carriage return | \r |
| Tab | \t |
| Backspace | \b |
| Form feed | \f |
The forward slash / may optionally be escaped as \/, but it is not required. Unicode characters can be written as \uXXXX using four hex digits.
JSON escape examples
The examples below show complete JSON string literals. The JSON Escape tool outputs the escaped content without enclosing quotes. Add one double quote at each end when inserting that output as a JSON string value, or use your language’s serializer.
Double quotes
Input text:
He said "hello"
Complete JSON string (including enclosing quotes):
"He said \"hello\""
Backslashes (Windows paths)
Input text:
C:\temp\file.txt
Complete JSON string (including enclosing quotes):
"C:\\temp\\file.txt"
Newlines and tabs
Input text:
Line 1
Line 2 Tabbed
Complete JSON string (including enclosing quotes):
"Line 1\nLine 2\tTabbed"
Unicode
A smiling face can be written as a Unicode escape:
"\u263A"
Characters outside the Basic Multilingual Plane use a surrogate pair when written with Unicode escapes, for example \uD83D\uDE80 for 🚀. They can also appear directly as Unicode text in JSON.
How to unescape a double-encoded JSON string
One encoding step turns text into a JSON string. A second encoding step turns that JSON string into another string, escaping its quotes and backslashes again. This example deliberately encodes twice so you can see each layer:
const text = 'He said "hello"';
const once = JSON.stringify(text);
const twice = JSON.stringify(once);
console.log(text); // He said "hello"
console.log(once); // "He said \"hello\""
console.log(twice); // "\"He said \\\"hello\\\"\""
const decodedOnce = JSON.parse(twice);
console.log(decodedOnce === once); // true: still serialized text
const decodedTwice = JSON.parse(decodedOnce);
console.log(decodedTwice === text); // true: original text
Decode only the layers your data format actually contains. If an API promises an object, inspect the parsed value: a string containing an object may mean the producer serialized it twice. Fix the producer when possible. Do not repeatedly call JSON.parse until it fails: a string such as "123" might be intentional text, not a number waiting to be decoded.
Are the extra backslashes real, or just the display?
A debugger or structured logger can display a string using an escaped representation. Seeing \\ on screen does not by itself prove that the underlying string contains two backslashes. Compare the actual characters and the serialized representation:
const path = String.raw`C:\temp\file.txt`;
console.log(path); // C:\temp\file.txt
console.log(JSON.stringify(path)); // "C:\\temp\\file.txt"
console.log([...path].filter(c => c === '\\').length); // 2
Both displays describe the same path. JSON.stringify escapes each backslash so the serialized value is valid JSON.
Why removing every backslash breaks JSON
In "C:\\temp\\file.txt", the backslashes preserve the path separators. In "He said \"hello\"", they preserve the quotation marks inside the string. A global backslash replacement can change the data or make it invalid. Use a JSON parser for a complete JSON string, or the JSON Unescape tool to inspect escaped content, then check the output against the format you need.
How to escape and unescape JSON in code
In production code you rarely escape by hand — every language has it built in.
JavaScript
const escaped = JSON.stringify(text); // escape
const back = JSON.parse(escaped); // unescape
Python
import json
escaped = json.dumps(text) # escape
back = json.loads(escaped) # unescape
Java (Jackson)
ObjectMapper mapper = new ObjectMapper();
String escaped = mapper.writeValueAsString(text);
String back = mapper.readValue(escaped, String.class);
C# (.NET)
using System.Text.Json;
string escaped = JsonSerializer.Serialize(text);
string back = JsonSerializer.Deserialize<string>(escaped);
Common escaping mistakes (and fixes)
- Suspected double escaping: first distinguish the actual value from the debugger’s escaped display. Decode an extra layer only when the input format requires it; see the worked example above.
- Invalid escape sequences: JSON only allows
\",\\,\/,\b,\f,\n,\r,\tand\uXXXX. Something like\x41is not valid JSON. - Raw newline inside a string: a literal line break in a string must be written as
\n.
Fix “invalid escape character in string”
These are JSON documents, not JavaScript source-code strings. The intended path contains literal backslashes:
Invalid: \p is not a JSON escape.
{"path":"C:\projects\report.json"}
Correct: escape each literal backslash.
{"path":"C:\\projects\\report.json"}
A different trap is {"path":"C:\temp"}: it parses, but \t becomes a tab. Valid syntax alone does not prove that the value is correct. If you construct this document in code, serialize the path value instead of assembling JSON by hand.
For a newline in a string, use an escape sequence inside the JSON document:
{"message":"Line 1\nLine 2"}
A literal line break between the string's quotes is invalid. If the error persists, use the JSON Validator to locate it; missing commas and unquoted keys need syntax repairs, not more escaping.
Escape or unescape JSON instantly
For ad-hoc work — reading an escaped value from logs, or escaping a blob of text to drop into a payload — use the JSON Escape tool or JSON Unescape tool. These tools process the conversion in your browser and handle quotes, backslashes, tabs, newlines and \uXXXX Unicode escapes. Escape produces content without enclosing quotes. Unescape produces readable text; when the input has enclosing quotes, the displayed output keeps those quotes, so it is not necessarily a valid JSON string literal. See the privacy policy for information about site analytics and third-party services. If the result still will not parse, run it through the JSON validator to find the exact line, or format the JSON to make the structure readable.
FAQs
How do I escape a JSON string?
Replace each special character with its backslash sequence (\", \\, \n, \t). In code use JSON.stringify (JavaScript) or json.dumps (Python), or paste the text into the JSON escape tool for a quick manual escape.
Is JSON escaping the same as URL encoding?
No. JSON escaping uses backslash sequences for JSON strings; URL encoding uses percent codes like %22 and %0A for URLs.
How do I escape a backslash in JSON?
Write a single backslash as two backslashes (\\). That is why C:\temp becomes C:\\temp when escaped.
Key takeaways
- Escape double quotes, backslashes and all control characters from U+0000 through U+001F.
- Never hand-roll escaping in production — use your language's serializer.
- When debugging a broken payload, escape or unescape the value and validate before sending. Escaping fixes string-encoding problems; other syntax errors may require different corrections.