gizmobench

JSON String Escape and Unescape

Paste a JSON string literal on the right and read it back as plain text on the left: \n becomes a line break, \" becomes a quote, \\ becomes one backslash and \u00e9 becomes é. What is different here is that the surrounding double quotes are a control with a name on it rather than a guess. Set Quotes to Included when you copied a whole literal out of a JSON file, and to Omitted when a log line or an error message handed you the inside of one without them. Both boxes are editable, so typing on the text side escapes instead, and the conversion runs exactly once in either direction: the literal \\n comes back as a backslash and an n, never as a line break. This reads one string literal and nothing else, so a literal that stops being valid is refused whole, with the position it stopped at, rather than handed back half decoded.

Direction
Quotes
Length
36 characters
Valid
Yes

Unescaping one whole JSON string literal, surrounding quotes included: each escape becomes the character it stands for, once, and anything after the closing quote is refused.

  • Quote and backslashboth are escaped
    "He said \"hi\" about C:\\tmp"
  • Tab and line breakwritten \t and \n
    "Tab:\tdone\nNext line"
  • An unterminated literalnothing closes the quote
    The literal that opens at position 1 is never closed: the text ends at position 5 with no closing double quote. Add a closing double quote, or set Quotes to Omitted if the text never had them.

Type in either box: editing the text side escapes, editing the literal side unescapes, and the conversion runs once either way. Length counts the characters of the converted side. Positions in a refusal count characters from the start of the box, beginning at 1.

Included or Omitted, and why it changes the answer. Included means the surrounding double quotes are part of the literal: it is what you have when you copy a value out of a JSON file, and it is what you want when you are about to paste the result back into one. Omitted means the inside of the literal on its own, which is what a log line, a stack trace or a shell variable usually hands you. Unescaping with the quotes Omitted refuses a raw double quote, because a literal cannot hold one, and unescaping with them Included refuses anything after the closing quote, because this reads exactly one literal.
Accuracy. Exact conversion by the JSON string rules, so what comes out parses as one string literal and reading it back returns the text you started with. Unescaping takes exactly one valid literal and names the position where an invalid one stops.

Common questions

How do I unescape a JSON string?
Paste it into the Escaped box with Direction set to Unescape, and the plain text appears on the left. Every escape becomes the character it stands for: \n a line break, \t a tab, \r a carriage return, \b a backspace, \f a form feed, \" a double quote, \\ a single backslash, \/ a forward slash and \u0041 the letter A. A pair such as \ud83d\ude00 is read as the one character it spells. If the text you have kept its surrounding double quotes, leave Quotes on Included; if it starts straight into the content, set Quotes to Omitted. Nothing is uploaded and there is no length limit: the reading happens in your browser.
Should the surrounding double quotes be included or not?
It depends on where the text came from, which is why it is a separate control rather than something this page guesses. A value copied out of a JSON file or an API response arrives as "like this", quotes and all, so Quotes is set to Included and the closing quote is what ends the literal. A value pulled out of a log line, a stack trace or an environment variable usually arrives without them, so Quotes is set to Omitted and the whole box is treated as the inside of a literal. The setting works the same way when escaping: Included wraps the result in quotes so it can be pasted straight into a JSON document, Omitted gives you the inside only, for somewhere that adds its own.
Which characters have to be escaped in a JSON string?
Three groups, and this tool escapes exactly those. The double quote and the backslash, because one would end the literal and the other starts an escape. The control characters below U+0020: five of them have a one letter escape (\b, \t, \n, \f, \r) and the rest are written \u00XX. And half of a character, an unpaired surrogate, which is written \uXXXX so the result is still well formed text. Everything else is left as it is, which means é, 日本語 and an emoji go out as themselves rather than as \u escapes. JSON allows both forms and they read back identically, so this tool keeps the shorter one. A forward slash is also left bare, exactly as JSON.stringify writes it, though \/ is accepted when reading a literal because the specification allows it.
Why does my text have \\n where a line break should be?
Because it was escaped twice, and this tool will not silently undo that. Unescaping is one pass: the literal "a\\nb" holds a backslash and an n, so one pass gives back a backslash and an n rather than a line break. That is the honest answer, since the text really does contain those two characters. If you want the line break, unescape the result a second time yourself by copying it back into the box: two passes are two decisions, and the tool never takes the second one for you. The same rule runs the other way, so escaping text that already carries escapes and reads as a whole literal says so in the line under the boxes before it happens.
Why is my literal refused, and what does the position mean?
A literal is refused whole rather than decoded halfway, and the message names the character position, counting from the start of the box beginning at 1. The refusals are: an escape that JSON does not have, such as \x; a \u without four hexadecimal digits after it; a backslash at the very end with nothing to escape; a raw line break, tab or other control character inside the literal, which JSON writes as \n, \t or \u00XX; a missing closing quote; a raw double quote when Quotes is set to Omitted; and more than one literal in the box, since this decodes exactly one. Each message says what to write instead. Whitespace before or after a literal that carries its own quotes is fine, because JSON treats it as insignificant.
Can I paste a whole JSON object or file into this?
No, and the refusal says so: this tool reads one string literal, so an object, an array or a bare number is turned away rather than half handled. That keeps the job small and exact. If you have a whole document to read, indent or check, the JSON formatter linked below is the page for it, and it names the cause and the line when a document will not parse. Come back here when you have one string value out of that document and you want to see what it really says, or when you have a piece of text that needs to become one.
Is anything I paste sent anywhere?
No. The conversion happens in your browser, so there is no server in this, no account and nothing logged. The two settings, the direction and the quoting, are remembered in this browser so the tool opens the way you left it, and the text in the boxes is never written to storage or anywhere else. The Start over button above the tool forgets those two settings again. There is no size limit either: a one line value is as welcome as a log file with a hundred thousand escapes in it, and the only ceiling is your own device.

Exact conversion by the JSON string rules, so what comes out parses as one string literal and reading it back returns the text you started with. Unescaping takes exactly one valid literal and names the position where an invalid one stops.