Maaz Zindani

Loading the index…

↑↓ move↵ openesc close3993 pages

Free tool · Developer tools

JSON escape

Escape any text so it can sit inside a JSON string, or unescape a JSON string back to plain text. You see what changed, a check that it parses back, and a Copy button.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

What do you want to do?
Options
JSON-escaped text

6 characters escaped46 characters in, 52 out

She said \"hello\"\nPath: C:\\temp\\notes\nCafé ✓ 😀
As a value in JSON
{"message": "She said \"hello\"\nPath: C:\\temp\\notes\nCafé ✓ 😀"}

Check: parsing it back gives your 46 characters exactly.

What was escaped
CharacterWritten asTimes
double quote\"2
line feed\n2
backslash\\2

How to use the JSON Escape

  1. Under What do you want to do? choose Escape text or Unescape a JSON string.
  2. Paste your text into the box. To unescape, you can paste the string with or without its outer double quotes.
  3. When escaping, optionally turn on Escape non-ASCII as \uXXXX, Escape / as \/ or Wrap in double quotes.
  4. Check the result and the table of what changed, then press Copy.

How do I escape a string for JSON?

A JSON string is text between double quotes, and a few characters cannot appear in it as they are. RFC 8259, section 7, says the double quote, the backslash and the control characters U+0000 to U+001F must be escaped. Everything else may be written as it is. So to put She said "hello" inside a JSON string, each inner quote gets a backslash in front: She said \"hello\". A real line break becomes the two characters \n, and a backslash is doubled, so C:\temp becomes C:\\temp.

The example in the box has three lines, quotes, a Windows path and some non-ASCII text. Escaped, it is She said \"hello\"\nPath: C:\\temp\\notes\nCafé ✓ 😀: 6 characters needed escaping. Below the result, the page shows the text as a value in a JSON object and parses it back to prove it is exactly your original, character for character.

The tool also escapes characters the RFC allows unescaped but that cause trouble elsewhere: the delete character, the C1 control characters, the line and paragraph separators U+2028 and U+2029, and lone surrogates. If you only need to turn text into a form that can sit in a web address, the URL encoder and decoder does that job instead.

Which characters must be escaped in JSON?

Three groups. The double quote and the backslash always need a backslash. The control characters below U+0020 need an escape too: five have short forms, and the rest use \u00XX with four hex digits. The short forms are \b (backspace), \f (form feed), \n (line feed), \r (carriage return) and \t (tab). The slash may be written \/ but does not have to be, and the option here is off by default; turn it on for text going into HTML script blocks.

Non-ASCII characters such as é or an emoji are allowed as they are in UTF-8 JSON. If a system needs pure ASCII, turn on Escape non-ASCII as \uXXXX. Each character becomes four hex digits, and characters beyond U+FFFF are written as two escapes, a surrogate pair. In the example, the last line becomes Caf\u00e9 \u2713 \ud83d\ude00, where the emoji is the pair \ud83d\ude00.

Common characters and how this tool writes them
CharacterLooks likeEscaped as
Double quote"\"
Backslash\\\
Line feednewline\n
Carriage returnreturn\r
Tabtab\t
Backspacenot printable\b
Form feednot printable\f
Bell (control U+0007)not printable\u0007
Slash (optional)// (or \/)

How do I unescape a JSON string, and why do I get an unexpected token error?

Switch to Unescape a JSON string and paste the text, with or without its outer quotes. Each sequence is decoded: \n becomes a line break, \" a quote, \\ a single backslash and \uXXXX the character with that code, including surrogate pairs. The table shows how many of each were decoded. The reverse of the example restores the original three lines exactly.

Most "unexpected token" and "bad escaped character" errors come from one of three mistakes: a double quote inside the string that was not escaped, a lone backslash (a Windows path pasted as C:\temp has \t in it, which JSON reads as a tab), or a made-up escape like \x41 or \' that JSON does not have. When the page finds one it names the character position, so you can fix that spot. Pasting JSON inside a JSON string works the same way: escape the whole inner document once, and its quotes become \". To check or reformat the whole document afterwards, use the JSON formatter.

A worked case: the text Tab:\t1 typed with a real backslash and the letter t is not a tab until JSON reads it. Escaped, the backslash is doubled, giving Tab:\\t1, so a parser later gives back the backslash and the t, not a tab. If instead you meant a tab, paste Tab:\t1 in Unescape mode and the result contains a real tab character. The two directions are exact inverses, which is why the escape result is always checked by parsing it back.

In code you would normally call your language's own serializer, for example JSON.stringify in JavaScript, and never build the escapes by hand. This page is for the other times: a value pasted into a config file, a test fixture, a curl body, a log line you need to read, or a string someone sent you with every quote already escaped.

Use it with

Questions

How do I escape a string for JSON?

Put a backslash before every double quote and backslash, and write line breaks and tabs as \n and \t. Other control characters use \u followed by four hex digits. This page does all of that and checks that it parses back.

Which characters must be escaped in JSON?

The double quote, the backslash and the control characters U+0000 to U+001F (RFC 8259, section 7). Everything else may be written as it is.

How do I unescape a JSON string?

Choose Unescape a JSON string and paste it, with or without outer quotes. Each escape sequence is turned back into its character, and an invalid one is reported with its position.

How do I put JSON inside a JSON string?

Escape the inner JSON text as a string, then use the result as the value. Its double quotes become \" and its line breaks become \n. The "As a value in JSON" box shows this.

Why do I get an unexpected token error?

Usually an unescaped double quote or backslash inside a string, or a raw line break. Switch on Unescape to see the exact character position of the first problem.

Does it send my text anywhere?

No. Escaping and unescaping run in your browser and nothing you type leaves your device.

How this page was made: the tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations were drafted with AI help and checked against the tool's own output.