Skip to content
FormatKit

Free online JSON string escaper

Escape JSON strings, and get them back

Paste text and this free tool returns it as a JSON string value — quotes and backslashes escaped, line breaks turned into \n, tabs into \t, and every remaining control character into \uXXXX — with no account and no size worth mentioning. Switches cover the cases JSON itself does not require but real consumers do: an ASCII-only transport, a parser that wants \/ for the forward slash, and an inline <script> block, where a raw </script> inside otherwise valid JSON ends the block early and breaks the page. The same page runs the other way, and if the string you unescape turns out to hold a whole JSON document, it gets indented for you.

  • 100% free
  • No signup
  • Escape and unescape
  • \uXXXX for non-ASCII
  • Safe inside <script>
JSON string value

The escaped value appears here as you type.

Nothing to convert yet. Paste something, or press Ctrl+V anywhere on this page.

How to escape a string for JSON

Pick a direction, match the switches to the consumer, copy the result.

  1. Choose which way you are going

    Text → JSON string takes ordinary text, a log line, a SQL query or a whole HTML fragment and makes it safe to sit inside a JSON value. JSON string → text does the reverse, and accepts the string with or without its surrounding quotes because half the time you copied it out of the middle of a document.

  2. Set the switches to match the consumer

    Wrap in quotes is on, so the output is a complete JSON value you can paste as-is. Turn on \uXXXX above ASCII when the transport is ASCII-only, escape / as \/ if a downstream parser expects it, and turn on “Safe inside <script>” when the JSON is going into an inline script tag — that one also escapes the angle brackets and the U+2028 and U+2029 separators.

  3. Read the tally, then copy

    The line under the panes breaks down what changed and how many of each — and when nothing needed escaping at all it says that too, which is worth knowing before you go hunting for the problem somewhere else. Going the other way, a string that turns out to hold a whole JSON document is indented underneath, because a double-serialised payload is the usual reason anyone is unescaping in the first place.

Technical specifications

Escapes always appliedThe double quote, the backslash, and every character below U+0020 — using \b \f \n \r \t where a two-character form exists, and \uXXXX for the other 27 control characters
Optional escapes\uXXXX for everything above U+007F, \/ for the forward slash, and \u003C \u003E \u0026 \u2028 \u2029 in the inline-script mode
Astral charactersWritten as UTF-16 surrogate pairs when ASCII output is on: 😀 (U+1F600) becomes 😀, matching what JSON.stringify with an ASCII post-pass produces
UnescapingRuns on the site's own JSON parser, so it rejects the same things a validator would and names them: \q is reported as not a JSON escape, \u12 as an incomplete one
QuotesOptional on output; on input they are optional too — a bare fragment is wrapped before parsing, and an error column is shifted back so it points at your text rather than the added quote
Double-encoded payloadsWhen the unescaped text is itself a JSON document it is re-indented with two spaces underneath, with its own copy button
Maximum input2,097,152 characters, escaped in about 40 ms; the escaper walks the string by code point rather than by UTF-16 unit
Price and processingNo charge, no account, no cap on how many strings go through; both directions run in this tab, so nothing you paste is transmitted

Frequently asked questions

Which characters must be escaped inside a JSON string?

Exactly three things, per RFC 8259 §7: the double quote, the backslash, and every character below U+0020. Everything else may appear literally, including the forward slash, the single quote, the ampersand and every emoji. Eight of the mandatory escapes have two-character forms — \" \\ \/ \b \f \n \r \t — and anything else that has to be escaped uses \u followed by four hex digits. A tool that escapes more than that is not making the JSON more valid, it is making it longer.

Why is my JSON valid and still breaking the page it is embedded in?

Because the HTML parser reads the page before the JavaScript parser does, and it is looking for </script>. If your data contains that sequence — in a stored HTML snippet, a code sample, a URL to a script file — the inline block ends there, mid-string, and everything after it becomes markup. The JSON was never wrong; it was in the wrong place unescaped. Escaping the angle brackets as \u003C and \u003E fixes it completely, because JSON reads them back as the same characters while the HTML parser sees no tag at all. That is what the “Safe inside <script>” switch does.

Do I need to escape a forward slash as \/?

No, and JSON has never required it. The escape is legal — \/ is one of the eight two-character forms — but a bare / is equally legal, and most encoders leave it alone. The convention exists for the same reason as the one above: escaping the slash breaks up the </script> sequence, so some libraries do it by default, PHP's json_encode being the loudest example. If you are comparing output byte for byte against another system, that single difference is usually the cause.

How is an emoji written with \uXXXX?

As two escapes, because \u carries a UTF-16 code unit rather than a code point. 😀 is U+1F600, above the 16-bit range, so it is written as the surrogate pair 😀 — and any decoder that treats those two as separate characters produces mojibake instead. Below U+FFFF it is one escape: é is \u00E9 and 東 is \u6771. None of this is necessary in a UTF-8 document, where all three can be written literally; it only matters when the transport is ASCII-only.

My value has \n in it but the line break never appears.

The value was escaped twice, so the reader is seeing a backslash and an n rather than a newline. It happens when a string that is already JSON gets serialised again: the \n becomes \\n, and one round of parsing gives back the two literal characters. Paste it into the unescape direction here and you will get the real line break; if the result is itself a JSON document, this page indents it underneath, which is the fingerprint of a webhook that put its payload in a string field.

What is U+2028 and why did it break my JavaScript?

It is the Unicode line separator, and until 2019 it was a line terminator in JavaScript but a perfectly ordinary character in JSON. That mismatch meant a JSON document containing one was not always valid JavaScript, so pasting it into a script — or serving it as JSONP — produced an unterminated string literal from nowhere. ES2019 fixed the language and the problem is now confined to older engines, but the character still arrives in text pasted out of PDFs and word processors, so the safe mode here escapes it and its paragraph-separator sibling U+2029.

Can I escape a whole JSON document this way?

You can, and it is a legitimate thing to want — that is exactly what embedding a payload in a string field means. Paste the document into the escape direction and the output is one JSON string value holding all of it, quotes escaped and line breaks turned into \n. Just be clear that you are creating a second level of encoding: whoever reads that field has to parse twice, and forgetting the second parse is the most common way it goes wrong.

About escaping strings for JSON

The rule is smaller than most people assume. RFC 8259 says a string is any sequence of Unicode characters wrapped in double quotes, except that the quote itself, the reverse solidus and the control characters U+0000 through U+001F must be escaped. That is the entire requirement. Eight escapes have a two-character shorthand — \" \\ \/ \b \f \n \r \t — and everything else uses \uXXXX. Note what is absent: the single quote needs nothing, because JSON strings are always double-quoted; the ampersand needs nothing; accented letters and emoji need nothing, because the document is Unicode. An escaper that rewrites those is padding the output, and a 27-character control-character table is the only part of the mandatory set anyone ever forgets.

The \u escape has a sharp edge worth knowing about: it carries a UTF-16 code unit, not a Unicode code point. That was a reasonable choice in 2001 and it means anything above U+FFFF has to be written as a surrogate pair, so a single emoji becomes two escapes and a naive character-by-character escaper that walks a JavaScript string by index will happily split one in half. Walking by code point avoids it, which is what this page does. The other historical wrinkle is U+2028 and U+2029: legal in a JSON string, and until ES2019 illegal in a JavaScript string literal, so a document that round-tripped fine through a parser exploded when it was pasted into a script. Those two characters are the reason the safe mode exists at all.

In practice most people arrive here holding a string that was encoded one time too many. A service serialises an object, puts the resulting text in a field of another object, and serialises that — so every quote is now \", every newline is \\n, and reading the payload takes two parses instead of one. It is not an error and it is occasionally deliberate, but it is invisible until something fails to unwrap it. This page detects it: unescape once, and if what comes out is itself a document, it is parsed and indented below the output by the same parser the prettifier uses. Escaping for a different destination follows completely different rules — a value going into markup needs entities, not backslashes, and applying both to the same string is how text ends up displaying as &amp;quot;.

Where the string is escaped

On your own machine, in this tab. The escaper is a loop over the characters you typed and the unescaper is the same JSON parser the rest of this site runs in the browser — neither needs a server, and no request is made while either works. API responses and log lines carry customer data more often than not, so the page is built so that pasting one is not a disclosure.