Escaping Special Characters in JSON Strings

Updated December 2026 · 7 min read

Most text can go straight into a JSON string between double quotes. A handful of characters can't, and need to be written as escape sequences that start with a backslash. Here's the complete list, why each one exists, and the situations where escaping trips people up.

Characters you must escape

The JSON standard (RFC 8259) requires escaping only three kinds of character inside a string:

  1. The double quote ", because it would otherwise end the string. Write it as \".
  2. The backslash \, because it starts every escape sequence. Write it as \\.
  3. Control characters (character codes 0 to 31), which include the line break, carriage return and tab. A JSON string must fit on one line, so a real line break inside one is an error. Write them as \n, \r, \t and so on.

Everything else, including single quotes, accented letters, Chinese characters and emoji, can appear as is.

Broken
{
  "quote": "She said "hello"",
  "poem": "Roses are red
Violets are blue"
}
Fixed
{
  "quote": "She said \"hello\"",
  "poem": "Roses are red\nViolets are blue"
}

Every JSON escape sequence

JSON allows exactly these escape sequences. A backslash followed by anything else is an error.

WriteTo getRequired?
\"Double quoteYes
\\BackslashYes
\nLine feed (new line)Yes, for line breaks
\rCarriage returnYes
\tTabYes, for tabs
\bBackspaceYes
\fForm feedYes
\/Forward slashNo, / works too
\uXXXXAny character by its 4-digit hex codeOnly for other control characters

Escapes from other languages don't work

JSON is stricter than JavaScript or Python strings. \', \x41, \0, \v, and \u{1F600} are all invalid in JSON, and the parser stops with an error like Bad escaped character.

Open an example with every common escape in the JSON tool

Unicode escapes and emoji

\u followed by exactly four hexadecimal digits stands for one character: "caf\u00e9" is "café". You never need this for normal text, since JSON files are UTF-8 and can hold any character directly. You'll still see it in output from tools that escape everything outside plain ASCII, such as Python's json.dumps by default.

Characters beyond the first 65,536, which includes most emoji, don't fit in four hex digits. They're written as a surrogate pair: two escapes in a row that together make one character.

JSON
{ "emoji": "\ud83d\ude00" }

That parses to 😀. If you see a broken symbol (�) instead, one half of a pair was lost, usually because a string was cut at a fixed length in the middle of an emoji.

Windows paths and regular expressions

Backslashes cause the most escaping bugs, because two common kinds of text are full of them. A Windows path needs every backslash doubled:

Broken
{ "log": "C:\Users\ada\notes.txt" }
Fixed
{ "log": "C:\\Users\\ada\\notes.txt" }

The broken version is especially sneaky. \U and \a are invalid escapes, so you get an error, but \n in \notes.txt is valid. If that were the only backslash, the JSON would parse without complaint and the path would contain a line break. Forward slashes ("C:/Users/ada/notes.txt") also work in most Windows programs and avoid the problem entirely.

Regular expressions stored in JSON have the same issue. The pattern \d+\.\d+ is written "\\d+\\.\\d+" in JSON: each backslash in the pattern becomes two.

JSON inside a JSON string

Sometimes a JSON document has another JSON document stored as a string, which is common in logs, message queues and webhook payloads. The inner JSON's quotes all get escaped:

JSON
{
  "event": "order.created",
  "payload": "{\"id\":5001,\"items\":[\"mug\"]}"
}

payload here is a string, not an object, so data.payload.id is undefined. Parse it a second time to get at the data:

JavaScript
const message = JSON.parse(text);
const payload = JSON.parse(message.payload);
payload.id;   // 5001

If you see backslashes multiplying, such as \\\", the data has been stringified more than once. Find where JSON.stringify or json.dumps is being called on something that's already a string, and remove the extra call.

JSON inside HTML

When you put JSON inside an HTML <script> tag, for example to pass initial data to a page, a string containing </script> would end the script tag early, even though it's perfectly valid JSON. That's a well-known source of cross-site scripting bugs. Escape < as \u003c when embedding JSON in HTML. Most server frameworks have a helper that does this for you, so look for that rather than inserting JSON.stringify output directly.

Let your code do the escaping

You should rarely escape JSON by hand. JSON.stringify in JavaScript and json.dumps in Python escape everything correctly, every time:

JavaScript
JSON.stringify({ quote: 'She said "hi"', path: "C:\\Users\\ada", note: "a\nb" });
// '{"quote":"She said \"hi\"","path":"C:\\Users\\ada","note":"a\nb"}'

Building JSON by joining strings together, like '{"name": "' + name + '"}', breaks the moment name contains a quote or a backslash. Build an object and stringify it instead. If you need to check a string you've escaped by hand, paste the JSON into the JSON tool: it marks any bad escape on its exact line.