How to Escape and Unescape JSON Strings (Quotes, Backslashes, Newlines & Unicode)

Unescaped characters are the number-one cause of broken JSON and Unexpected token 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

JSON defines exactly seven characters that must be escaped inside a string:

CharacterEscaped 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

Double quotes

Input text:

He said "hello"

Escaped JSON string:

"He said \"hello\""

Backslashes (Windows paths)

Input text:

C:\temp\file.txt

Escaped JSON string:

"C:\\temp\\file.txt"

Newlines and tabs

Input text:

Line 1
Line 2	Tabbed

Escaped JSON string:

"Line 1\nLine 2\tTabbed"

Unicode

A smiling face can be written as a Unicode escape:

"☺"

Emojis may require surrogate pairs, for example 🚀 for 🚀.

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, \t and \uXXXX. Something like \x41 is not valid JSON.
  • Raw newline inside a string: the most common cause of Unexpected token — 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 — a browser tool is faster than writing a script. Our free JSON Escape / Unescape tool handles quotes, backslashes, tabs, newlines and \uXXXX Unicode, runs 100% in your browser (nothing is uploaded), and the Unescape button reverses it. 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 the seven special characters; everything else can stay as-is.
  • 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. That removes the vast majority of Unexpected token errors.

Related articles