On-device AI — professional-grade, fully private.

Documents

JSON Escape and Unescape: Which Characters Need Escaping and How to Fix Double-Escaped Strings

JSON escape and unescape explained: which characters JSON requires escaping, how to put JSON inside a string, and how to fix double-escaped quotes.

Updated November 8, 20269 min read

To JSON escape a piece of text, you put a backslash in front of every double quote and every backslash, replace line breaks and tabs with \n, \r and \t, and wrap the result in double quotes. Unescaping does the reverse: \" becomes a quote again and \n becomes a real line break. That is nearly the whole rule set, and the JSON Escape / Unescape tool applies it exactly, in either direction. Most trouble comes from what happens around those rules: JSON pasted inside another JSON string, backslashes that multiply every time a value is saved, and characters that are legal in JSON but break the place you paste them into. This guide covers each of those.

What JSON escape means, and which characters need it

A JSON string starts and ends with a double quote. Anything inside that could be mistaken for the end of the string, or for the start of an escape, has to be written differently. The JSON standard (RFC 8259) makes exactly three groups of characters mandatory to escape: the double quote, the backslash, and every control character from U+0000 to U+001F, which includes the newline and the tab. Everything else, including accented letters, emoji, apostrophes and the forward slash, may appear as it is.

CharacterWritten in JSON asRequired?
Double quote "\"Yes, otherwise it ends the string
Backslash \\\Yes, otherwise it starts an escape
Newline (line feed)\nYes, a real line break inside a string is invalid
Carriage return\rYes
Tab\tYes
Backspace, form feed\b, \fYes
Other controls U+0000 to U+001F\u0000 to \u001fYes, there is no short form
Forward slash // or \/No, both are valid
Non-ASCII such as é or 😀as is, or \u00e9 and the pair D83D DE00No, both are valid
Apostrophe 'as isNo, and \' is not a valid escape

The last row catches a lot of people. JavaScript and Python accept \' inside a string, so it looks natural, but JSON does not define it. A parser that meets \' stops with an error. In JSON an apostrophe needs no escape at all, and single quotes can never be used to wrap a string.

Escape or unescape: which one you need

Your situationDirectionWhat you get
A multi-line SQL query, certificate or template has to become one JSON valueEscapeOne line with \n where the breaks were
A string full of quotes has to go into a config file or a test fixtureEscapeEvery " written as \"
A log line or API field reads as one row of \n and \"UnescapeThe readable text, with real line breaks
A field holds a whole JSON document as a stringUnescapeThe inner document, ready for the JSON Formatter
You need to check that escaped text says what you thinkEscape, then SwapA round trip that should give back the original exactly

Escaping is not encryption and it is not compression. It only changes how special characters are written so a parser reads them as text. If you need to turn binary data such as an image into text, that is a different job, covered in what Base64 encoding is. If the text is going into a web address, it needs percent-encoding from the URL encoder instead.

How to escape or unescape a JSON string, step by step

1

Choose the direction

Open the JSON escape tool and pick Escape to turn plain text into a JSON string literal, or Unescape to turn a JSON string back into text.

2

Add your text

Paste into the box and press Use this text, or drop a .json, .txt, .log, .ndjson or similar text file onto the drop area. UTF-8 and UTF-16 files are both recognised.

3

Set the options

For Escape, Wrap the result in quotes is on by default; turn it off when you will paste between quotes that already exist in your code. The other three toggles are covered below. For Unescape, leave Layers to peel on Auto (up to 10) unless you want to stop part way.

4

Run it and check the counts

Press Escape or Unescape. The result appears beside your source with character and byte counts and a growth ratio, which matters when the value has to fit a column or a payload limit.

5

Copy, download or swap

Copy the result, download it as a text file, or press Swap to send it back into the input in the other direction and confirm you get your original text back.

Escape text to a JSON string or decode one back, with nested layers peeled automatically. Free: one file or pasted text up to 10 MB, with every option. Pro: no fixed size cap and up to 20 files at once, downloaded as one ZIP. It all runs on your device, not our servers.

Escape or unescape JSON

Putting JSON inside a JSON string

Sometimes a whole JSON document has to travel as a single string: a webhook payload stored in a database column, a settings object passed through a queue that only accepts text, or a message field in a log event. To do that you escape the document. The object {"name":"Ana"} becomes the string "{\"name\":\"Ana\"}". Every inner quote gains a backslash, and the whole thing gains an outer pair of quotes.

Before escaping a document, make sure it is valid, because escaping an object with a missing comma gives you a perfectly valid string that holds a broken document. Run it through the JSON Validator first. If size matters, strip the indentation with the JSON Minifier before escaping: each line break in a pretty-printed document turns into a two-character \n, and indentation spaces are kept, so an escaped pretty document is noticeably larger than an escaped minified one.

Going the other way, when an API returns a field whose value starts with "{\", you are looking at an embedded document. Unescape it once and you get real JSON back, which you can then format and read.

Double escaping: why the backslashes keep multiplying

Each time a value is escaped, every existing backslash is doubled and every quote gains one more. A newline that is \n after one round becomes \\n after two and \\\\n after three. Unescape that once and it still looks escaped, which is why people think the tool did not work. It did; it removed one layer of several.

Leave Layers to peel on Auto and the tool keeps going while the result is still a quoted JSON string, up to ten layers, then tells you how many it removed. Set a number from 1 to 10 if you want to stop at an inner layer, for example to hand an embedded document to the formatter rather than decode it further. Plain text that only happens to contain \n is never peeled, because a layer is removed only when what is left really parses as a JSON string.

The usual causes of double escaping, so you can fix it at the source:

  • Serialising twice. Calling JSON.stringify in JavaScript or json.dumps in Python on something that is already a JSON string wraps it again. Serialise the object once, at the last step.
  • Logging libraries. Structured loggers escape the message, and some pipelines then escape the whole event a second time when they forward it.
  • Escaping by hand and then with code. Adding \" yourself and then passing the text through a serialiser gives \\\".
  • Copying from a debugger or console. Some consoles show strings in their escaped form. Copying that display and escaping it again adds a layer.
  • Windows paths. C:\Users\Ana must be written as C:\\Users\\Ana in JSON. Pasting it unescaped gives \U, which is not a valid escape, and escaping it twice gives four backslashes per separator.

Escaping for the place the string is going

All of the following produce valid JSON either way. The three optional toggles in the tool exist because the destination sometimes cares.

OptionTurn it on whenCost
Escape non-ASCII as \uXXXXThe value passes through a system that is not reliably UTF-8, or you want pure ASCII in source controlé grows from 2 bytes to 6; an emoji from 4 bytes to 12, as two escapes
Escape forward slashes as \/The string goes inside an HTML script block, where </script> in a string would end the tag earlyOne extra byte per slash
Escape line separators (U+2028, U+2029)The literal is pasted into JavaScript source for older engines, where these two invisible characters were a syntax errorSix bytes instead of three per character

Pasting into code is its own case. A JSON string literal is usually also a valid string in JavaScript, Python and Go, so you can paste the escaped result between your own quotes with the wrap option turned off. In a shell, wrap a JSON body in single quotes and the double quotes inside need no further escaping; on the Windows command prompt there are no single-quoted strings, so every inner double quote has to be written as \" for the command line, which is a second, separate layer on top of JSON.

When unescape fails: what the error means

MessageUsual causeFix
The string is never closedThe value was cut off by a log length limit or a short copyPaste the whole value, or add the closing quote
Unknown escape sequenceA backslash before a letter JSON does not define, such as \' or \U in a Windows pathDouble the backslash, or escape the original text instead
Incomplete \u escape\u is not followed by exactly four hex digits\u12 is invalid; write \u0012
Raw control characterA real line break or tab sits inside the string, often after copying from a terminalRun Escape on the text instead
Extra content after the stringTwo strings side by side, or text after the closing quoteConvert one value at a time
That is a JSON document, not a stringYou pasted an object or an arrayUse the formatter or validator; Unescape works on one string

Each message comes with the line and column and the surrounding text, so on a long log line you can see exactly which backslash is the problem rather than searching for it.

Frequently asked questions

How do I escape double quotes in JSON?

Put a backslash before each one, so " becomes \". Any backslash already in the text must be doubled first, or the result will be read differently. Pasting the text into the Escape mode does both and wraps the result in quotes.

How do I unescape a JSON string online?

Choose Unescape, paste the string with or without its surrounding quotes, and run it. Leave the layer setting on Auto if the value may have been escaped more than once; the tool reports how many layers it removed.

Why does my JSON have a backslash before every quote?

The value was stored as a string rather than as an object, usually because it was serialised twice. Unescape it to get the inner JSON back, then fix the code that serialises it so it runs only once.

Can I use single quotes or \' in JSON?

No. JSON strings must use double quotes, and \' is not a valid escape. An apostrophe inside a double-quoted string is fine as it is and needs no backslash.

Do I need to escape forward slashes in JSON?

No. Both / and \/ are valid and mean the same thing. Escaping them only helps when the JSON is placed inside an HTML script tag, where the text </script> would otherwise close the tag.

How large a text can I escape for free?

Free handles one file or pasted text up to 10 MB, with every option and every unescape depth. Pro removes the fixed cap, works up to what your device can handle, and takes up to 20 files at once.

Keep reading

More in Documents →