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 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)
- Double escaping (backslashes everywhere): the string was escaped twice as it passed through systems or loggers. Unescape once to normalize, then escape again only if needed.
- 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.
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.