Escape and unescape JSON strings
Paste text to get it back escaped for JSON, or swap direction and paste an escaped string to read it. It updates as you type, and nothing leaves this tab.
How to use it#
- Paste the text you want to put inside a JSON string.
- Tick Wrap in quotes if you want a complete JSON value, and Non-ASCII as \u if the destination mangles UTF-8.
- Press the swap button to unescape instead, then copy the result from the box under it.
What has to be escaped, and what does not#
A JSON string is text between double quotes, so two characters can never appear in it as they are: the double quote, which would end the string, and the backslash, which starts an escape. Those become \" and \\. Control characters, the invisible ones below the space, are the third group. A line break becomes \n, a tab \t, a carriage return \r, and the rarer ones are written as \u followed by four hex digits.
Everything else may be written as it is, including accented letters, Chinese characters, and emoji. JSON is defined as UTF-8 text, and any parser that follows the standard reads them. A forward slash may be escaped as \/ but never has to be; this tool leaves it alone when escaping and accepts it when unescaping.
Where escaped JSON turns up#
The usual reason to unescape is a JSON document that has been stored inside another one as a string: a log line holding a request body, a message queue payload, or a configuration value in an environment variable. It arrives as one long line full of \" and \n. Unescaping it gives you the original, and if that turns out to be JSON itself, the page says so and the JSON formatter will lay it out.
The usual reason to escape is the other direction: putting a block of text, a multi-line SQL query, or a snippet of HTML into a JSON file or an API request by hand. Typing the escapes yourself is where a stray quote breaks the whole document. To send a whole JSON document as a string, as some webhook and queue fields expect, paste the document here and escape it with Wrap in quotes.
In JavaScript terms, escaping with Wrap in quotes gives what JSON.stringify returns for a string, and unescaping does what JSON.parse does to one. That is also the fix for a JSON document that arrives with a backslash before every quote: unescape it once and the backslashes go, leaving JSON you can parse.
Reading the error when unescaping fails#
The message names the character and its position in the box. A double quote with no backslash before it would have ended the string early, so it is reported rather than guessed at. A real line break in the middle is the most common mistake, usually from text that was unescaped once already and is being run through again; JSON needs it written as \n.
An escape JSON does not have, such as \x41 or \a, is rejected too. Those come from other languages. JavaScript and Python accept \x, but JSON allows only \u with exactly four hex digits.
Frequently asked questions#
Do I need to include the quotes?
#
Either works when unescaping. If what you paste starts and ends with a double quote, those are read as the string's own quotes and the page says so; otherwise the whole box is taken as the inside of a string. When escaping, Wrap in quotes gives a complete JSON value, and leaving it off gives the part that goes between quotes you have already typed.
When should I escape non-ASCII characters?
#
Only when the text will pass through something that does not handle UTF-8 properly, such as an old database column, a system that expects Latin-1, or an email gateway. With the option on, é becomes \u00e9 and an emoji becomes two escapes, because JSON counts in UTF-16 units. The meaning is identical, so any JSON parser reads both forms the same.
How is this different from URL encoding?
#
They protect different containers. JSON escaping keeps a string intact inside a JSON document, so only quotes, backslashes, and control characters change. URL encoding keeps text intact inside a web address and turns spaces, ampersands, and non-ASCII text into percent codes. For that, use the URL encoder.
Why did my text come back with \\ in it?
#
Because it already held a backslash, and a literal backslash in JSON is written as two. A Windows path such as C:\temp becomes C:\\temp. Unescaping turns the pair back into one.
Is my text sent anywhere?
#
No. Escaping and unescaping run in this tab, so a payload holding tokens or customer details never leaves your machine.