How to Escape and Unescape JSON Strings?

A JSON string is text between two double quotes. Most characters can go inside as they are. A few cannot. They would end the string too early or confuse the parser. (A parser is the code that reads JSON text and turns it into data.)

Escaping means writing those characters in a special form that starts with a backslash. Unescaping is the opposite. This guide shows which characters need it. It also shows how to fix two common problems: Windows paths, and JSON full of \" in your logs.

Quick answer: In a JSON string, you must escape the double quote as \" and the backslash as \\. You must also escape control characters, such as a line break (\n) or a tab (\t). To unescape, turn those codes back into the real characters. For example, use JSON.parse.

Why do some characters need escaping?

The parser reads a string from the first " to the next ". So if your text has a quote in it, the parser thinks the string has ended:

{ "quote": "She said "hi"" }        ← invalid
{ "quote": "She said \"hi\"" }      ← valid

The backslash (\) is the escape character. It tells the parser: “the next character is special, don’t treat it the normal way.”

Which characters must be escaped in JSON?

Only three kinds of characters must be escaped:

  1. The double quote "
  2. The backslash \
  3. Control characters. These are invisible characters like line breaks and tabs. You cannot put a real line break inside a JSON string. You have to write \n instead.

Here is the full list of escape codes JSON understands:

\"      double quote
\\      backslash
\/      forward slash (allowed, but never required)
\b      backspace
\f      form feed
\n      line break (newline)
\r      carriage return
\t      tab
\uXXXX  any character, by its 4-digit code

Everything else can go in as it is. That means letters, digits, accents, emoji, and symbols like <, & and '.

Note that \' is not a valid escape in JSON. A single quote never needs escaping.

What does \uXXXX mean?

After \u come exactly four hexadecimal digits. Hexadecimal is a way to write numbers with 0–9 and A–F. The four digits are the character’s number in Unicode, the standard list of all characters. For example, \u00e9 is “é”.

Emoji and some rare characters need two of these codes in a row. This is called a surrogate pair. The rocket emoji 🚀 is \ud83d\ude80.

You only need \u for control characters that have no short code. Accents and emoji are allowed in JSON as they are.

Escape them only if the system that gets your JSON can’t handle UTF-8 text. (UTF-8 is the most common way to store text with accents and emoji.)

How to escape Windows file paths in JSON

Windows paths use backslashes, and every backslash in a JSON string must be doubled:

{ "path": "C:\\Users\\ada\\new" }

Forgetting this causes two different problems:

  • "C:\Users" gives an error, because \U is not a known escape code.
  • "C:\new" gives no error, but \n becomes a line break. Your path is now wrong, and nothing warns you.

Many Windows programs also accept forward slashes, like "C:/Users/ada/new". Forward slashes need no escaping.

How to escape a JSON string in JavaScript and Python

In code, don’t escape by hand. Let the JSON library do it. It always gets every character right.

JavaScript

const text = 'She said "hi"\nThen left';

console.log(JSON.stringify(text));
// "She said \"hi\"\nThen left"

console.log(JSON.stringify({ note: text }));
// {"note":"She said \"hi\"\nThen left"}

// Unescape: JSON.parse turns the codes back into characters
console.log(JSON.parse(JSON.stringify(text)) === text); // true

Python

import json

text = 'She said "hi"\nThen left'
print(json.dumps(text))
# "She said \"hi\"\nThen left"

print(json.loads('"Caf\\u00e9"'))
# Café

By default, Python’s json.dumps also escapes every non-English letter. So “Café” becomes "Caf\u00e9". Add ensure_ascii=False to keep the letters as they are.

For a quick one-time job, paste your text into the JSON Escape tool. It escapes quotes, backslashes, line breaks, tabs and other control characters.

It can also put quotes around the result. Or it can escape every non-ASCII character (anything outside basic English letters, digits and symbols) as \uXXXX.

JSON inside JSON: fixing double-escaped strings

Sometimes a whole JSON document is stored as a string inside another JSON value. You often see this in log files and message queues. You also see it in API fields with names like body or payload.

Each time JSON is turned into a string, it gets one more layer of backslashes:

{"id":1}                      ← the JSON itself
"{\"id\":1}"                  ← stored as a string once
"\"{\\\"id\\\":1}\""          ← stored as a string twice

A log line often looks like this:

{"level":"info","message":"{\"id\":1,\"name\":\"Ada\"}"}

To read it, you need to remove the layers. Paste it into JSON Unescape. You can paste it with or without the outer quotes.

Choose “All levels”, and the tool keeps unescaping until the text stops changing. If the result is JSON, the tool formats it for you.

In code, each JSON.parse call removes one layer. Keep calling it while the result is still a string.

Do you want to go the other way, and put a JSON document inside a string on purpose? Use JSON Stringify. It turns a whole document into one quoted, escaped string.

This is like calling JSON.stringify twice in JavaScript. It is useful for test files, environment variables and API fields that expect JSON as text.

Does your JSON still give an error after unescaping? Our list of common JSON errors covers the usual causes.

Common questions

Do I need to escape forward slashes in JSON?

No. \/ is allowed, and some tools write it. For example, PHP’s json_encode does this by default. But a plain / is just as valid.

Do single quotes need escaping in JSON?

No. A single quote can go inside a JSON string as it is. In fact, \' is not valid JSON. It will cause an error.

How do I put a line break in a JSON string?

Write \n. A real line break inside the quotes is not allowed. When the parser reads the string, it turns \n into a real line break.

Why is my JSON full of \" characters?

It has been turned into a string, often more than once. This happens when JSON is stored inside another JSON value, or written to a log. Unescape it once for each layer to get the original JSON back.