How do I escape double quotes in a JSON string?

Dark navy cover card with the title 'How do I escape double quotes in a JSON string?' in white and a green accent bar

To escape double quotes in a JSON string, put a backslash in front of each quote that belongs to the text: She said "yes" becomes "She said \"yes\"". The backslash itself also has to be escaped, as \\. In practice you rarely type these by hand: JSON.stringify in JavaScript or json.dumps in Python adds them for you.

The rule for double quotes in JSON strings

JSON strings start and end with a double quote, so a double quote inside the text has to be marked as text with a backslash. RFC 8259, the JSON standard, lists \" as the escape for a quotation mark.

Broken, then fixed:

{"quote": "She said "yes""}
{"quote": "She said \"yes\""}

In the first line the parser sees the string end right before yes, then hits a word where it expects a comma. Node.js reports Expected ',' or '}' after property value in JSON. The second line parses to She said "yes", backslashes gone.

Single quotes need nothing. JSON only uses double quotes as delimiters, so the apostrophe in it's is plain text. The reason for that rule is in why JSON requires double quotes.

Which characters must you escape in JSON?

You must escape exactly three groups of characters in a JSON string: the double quote, the backslash and the control characters U+0000 through U+001F. That list comes straight from section 7 of RFC 8259. Everything else, including non-ASCII letters and emoji, can appear as is.

Escape Character Required?
\" double quote Yes
\\ backslash (reverse solidus) Yes
\n line feed Yes, as a control character
\r carriage return Yes, as a control character
\t tab Yes, as a control character
\b backspace Yes, as a control character
\f form feed Yes, as a control character
\/ forward slash (solidus) No, optional
\uXXXX any character by its hex code Only for other control characters

The \u form works for any character. The RFC's example writes the G clef symbol (U+1D11E) as "\uD834\uDD1E", a pair of escapes. A raw line break inside a string is the most common slip after quotes: paste a multi-line message into a JSON value and it fails until each break becomes \n.

Let a serializer escape double quotes for you

The safest way to escape double quotes in JSON is to never do it by hand: build the data as a normal object and let the language's JSON encoder write the string. Code that glues JSON together with string concatenation is a bug waiting for the first user who types a quote.

JavaScript

JSON.stringify escapes quotes, backslashes and control characters for you:

JSON.stringify({ quote: 'She said "yes"' });
// {"quote":"She said \"yes\""}

MDN adds one edge case: modern engines escape lone surrogates (U+D800 to U+DFFF) as \u sequences, so the output stays valid UTF-8. The reverse direction is covered in JSON.stringify vs JSON.parse.

Python

json.dumps does the same job. The Python json docs show json.dumps("\"foo\bar") printing "\"foo\bar", with the quote escaped as \" and the backspace as \b. A single backslash comes out as "\\".

ensure_ascii is on by default, so Python also escapes every non-ASCII character as \u codes. Pass ensure_ascii=False to keep accented letters readable. The docs say quotes, backslashes and control characters are still escaped either way.

Double-escaped JSON: when quotes get two layers

Double escaping happens when JSON text is treated as a plain string and encoded a second time. Each layer adds its own backslashes, so {"a":"b"} stringified again becomes:

JSON.stringify(JSON.stringify({ a: 'b' }));
// "{\"a\":\"b\"}"

You see this in API responses that store a JSON payload inside a string field, in log lines, and in message queues. The quick test: if the whole value starts with "{ and every inner quote has a backslash, it's a JSON string that contains JSON. Parse it once to get the string, then parse that string to get the object.

Code adds another layer on top. A JSON string written inside a JavaScript or Java string literal needs the backslashes doubled again, because the language reads \" first and hands the parser only ". When the escapes stop making sense, count the layers: file, language literal, then JSON. Each one takes a backslash away.

Undo double escaping without code

The JSON unescape tool on OnlineViewer cleans double-escaped or stringified JSON so you can read it as normal JSON. It runs in the page, and the tool code makes no network requests with what you paste.

Unescaping only reverses the extra layer. If the original JSON was invalid before it was stringified, the cleaned result is still invalid. Run it through a validator next, or see how to fix invalid JSON for the usual repairs. If the parse error is about the input stopping early instead, unexpected end of JSON input covers that case.

FAQ

How do you escape a double quote in JSON?

Put a backslash before it. Inside a JSON string, a literal double quote is written as \", so She said "yes" becomes "She said \"yes\"".

Do single quotes need escaping in JSON?

No. JSON strings are delimited by double quotes only, so a single quote inside a string is an ordinary character. MDN's JSON.stringify examples show single quotes left as they are.

Which characters must be escaped in a JSON string?

RFC 8259 says three groups must be escaped: the quotation mark, the backslash (reverse solidus), and the control characters U+0000 through U+001F. Everything else may be escaped but doesn't have to be.

Why does my JSON have backslashes before every quote?

It was probably stringified twice. Running JSON.stringify on a string that already holds JSON escapes all of its quotes, giving output like "{\"a\":\"b\"}". Parse it once to get the real JSON back.

Sources