The bug that made me build this tool was maddeningly small. I was stuffing an HTML email template - a few hundred lines with quotes, line breaks, and the odd backslash - into a JSON field to send to an API, and the request kept coming back as a 400. The template was fine. The JSON was fine on its own. But the moment the two met, an unescaped double quote three hundred characters in silently closed the string early, and everything after it turned into garbage the parser could not make sense of. I had pasted valid text into a valid container and produced an invalid document.
That is the whole problem JSON escaping solves, and it is worth understanding rather than working around. This guide covers what escaping actually does, which characters must be escaped and why, how unescaping reverses it, and how to use the free JSON Escape / Unescape tool to move arbitrary text in and out of JSON strings without a single silent failure. It sits alongside the other data utilities I have written about in the ultimate guide to JSON tools.
TL;DR: A JSON string cannot contain a raw double quote, a raw backslash, or raw control characters like newlines and tabs. Escaping replaces each with a backslash sequence -
"becomes\", a newline becomes\n, a tab becomes\t, and anything else below U+0020 becomes\uXXXX. Unescaping turns those sequences back into the original characters. The JSON Escape / Unescape tool does both in your browser, with options to wrap the result in quotes, escape non-ASCII as\uXXXX, or escape the forward slash.
What does it mean to escape a string for JSON?
JSON has a precise, published grammar - it is defined by RFC 8259 and by the ECMA-404 standard - and the rules for what may appear inside a string are strict. A JSON string is a run of characters wrapped in double quotes. Because the double quote marks the start and end of the string, a double quote inside the text would end it prematurely. Because the backslash begins an escape sequence, a raw backslash would be misread as the start of one. And because JSON strings are meant to be a single logical line of text, literal control characters - newlines, tabs, carriage returns - are not allowed to sit inside them unescaped.
Escaping is the process of replacing each of those troublesome characters with a two-character backslash sequence that JSON understands. A double quote becomes \". A backslash becomes \\. A newline becomes \n. The result is text that looks a little uglier but is now safe to drop between two quote marks without breaking the surrounding document. The meaning is identical - a JSON parser reading \n produces a real newline again - but the representation is now legal.
This is fundamentally different from formatting a JSON document. A formatter arranges the structure of an entire object: indentation, key ordering, whitespace between elements. Escaping operates one level down, on the contents of a single string value. You reach for the JSON Formatter when you have a whole document to pretty-print or validate, and for the JSON Escape / Unescape tool when you have a lump of raw text you need to fit inside one field.
Which characters actually need escaping?
The JSON specification is refreshingly specific here. Inside a string, exactly two characters must always be escaped, plus the entire range of control characters.
The two mandatory single-character escapes are the double quote (\") and the backslash (\\). Beyond those, JSON defines short escapes for five common control characters: backspace (\b), form feed (\f), newline (\n), carriage return (\r), and horizontal tab (\t). Any other control character - anything with a code point below U+0020 that does not have a short form - must be written as a \u escape followed by four hexadecimal digits, so a null byte becomes \u0000 and the "unit separator" becomes \u001F.
Two more characters are optional to escape, and this is where confusion creeps in. The forward slash may be written as \/, but it does not have to be - a plain / is perfectly valid JSON. And any character above the ASCII range, like an accented é or an emoji, may be written literally in a UTF-8 document or escaped as \uXXXX; both are legal. The JSON Escape / Unescape tool always handles the mandatory escapes, and gives you toggles for the two optional ones so you can match whatever your target system expects.
Should I escape the forward slash and non-ASCII characters?
These two optional escapes exist for real reasons, even if you rarely need them.
The forward-slash escape matters in exactly one common situation: when JSON is embedded inside an HTML <script> tag. The browser's HTML parser looks for the literal sequence </ to find the end of a script block, so a string containing </script> in your JSON can prematurely close the tag and break the page. Writing the slash as <\/script> - still valid JSON, still the same string once parsed - avoids that. Outside of inline scripts, you can leave slashes alone, which is why the tool leaves the forward slash unescaped by default.
Escaping non-ASCII characters as \uXXXX is about portability rather than correctness. If your JSON might pass through a system that mangles UTF-8 bytes - an old logging pipeline, a misconfigured proxy, a channel that assumes ASCII - turning every character above the ASCII range into a pure-ASCII \u escape guarantees it survives intact. The output is bulkier and less human-readable, so it is off by default, but the "Escape Unicode" toggle is there for the moment you need bulletproof transport. A code point like é becomes \u00e9, and a JSON parser reconstructs the original character on the way out.
How does unescaping work, and when do I need it?
Unescaping is the exact inverse: it walks through an escaped string and turns each backslash sequence back into the raw character it represents. \n becomes a real newline, \t becomes a tab, \" becomes a double quote, and \uXXXX becomes the character at that code point. You need it whenever you are on the receiving end - you have copied a string value straight out of a JSON file, a log line, or an API response, and you want to read the original text without a screenful of backslashes in the way.
A good unescaper does two things a naive find-and-replace does not. First, it strips a single pair of surrounding double quotes if the whole value is quoted, so you can paste "line one\nline two" straight from a JSON document and get clean text back. Second, it validates as it goes: an invalid escape sequence like \x, or a stray backslash at the very end of the string, is a real error, and the JSON Escape / Unescape tool reports its position rather than guessing and producing silently wrong output. Getting a clear "invalid escape at position 14" beats getting text that looks almost right but is subtly corrupted.
The key property to trust is that escaping and unescaping are exact inverses. Escape any text and then unescape the result and you get back precisely what you started with, character for character. That round-trip guarantee is what makes it safe to use the tool as a routine step in a pipeline rather than a risky transformation you have to double-check by hand.
Escaping versus related string operations
JSON escaping is one of a family of "make this text safe for that context" transforms, and it helps to keep them straight. Here is how it compares to the neighbours developers most often confuse it with.
| Operation | Protects against | Example | Tool |
|---|---|---|---|
| JSON escape | Breaking a JSON string with quotes, backslashes, or control chars | he said "hi" → he said \"hi\" |
JSON Escape / Unescape |
| HTML entity encode | Breaking HTML or enabling XSS with <, >, & |
<b> → <b> |
HTML Entities |
| URL percent-encode | Breaking a URL with spaces or reserved characters | a b → a%20b |
- |
| JSON format/validate | Structural errors across a whole document | minified → pretty-printed | JSON Formatter |
| JSON → YAML | Moving a config between serialization formats | {"a":1} → a: 1 |
JSON to YAML |
The pattern to notice is that each escape targets a different container. HTML entities keep text from breaking HTML; percent-encoding keeps it from breaking a URL; JSON escaping keeps it from breaking a JSON string. Using the wrong one - HTML-encoding something that needs JSON escaping - leaves the real problem unsolved and adds a new layer of noise. Because all of this runs on text you might not want to hand to a third-party server, I keep every one of these tools client-side, which is the argument I made in full in the data privacy guide for online tools.
What are the common mistakes when embedding text in JSON?
The single most common mistake is the one that cost me an afternoon: pasting multi-line text with unescaped quotes directly into a JSON field and assuming that because both parts are valid, the whole is valid. It is not - the container has rules the contents must obey.
A close second is double-escaping. If a string has already been escaped once and you escape it again, every \n becomes \\n and your newlines turn into a literal backslash-n that no parser will convert back. This usually happens when a value passes through two systems that each helpfully escape it. If your output has suspicious runs of \\ where you expected single backslashes, you have probably double-escaped, and unescaping once will fix it. The third trap is forgetting that a Windows path like C:\Users\me is full of backslashes, each of which must become \\ - an unescaped path in JSON is a classic source of "why is my JSON invalid" confusion.
The fix for all three is the same: let the tool do the escaping exactly once, at the boundary where raw text meets JSON, and read the character-count summary it shows to sanity-check that something changed. When you need to hand-verify, escape with "wrap in quotes" enabled and paste the result into the JSON Formatter - if it parses as a valid string, your escaping is correct.
A worked example: embedding an HTML snippet in JSON
Let me walk through the exact situation that started this, because seeing the transformation once makes the rule stick. Say you want to send this fragment as the body field of an API request:
<p>Hi "there",</p>
<p>Visit https://example.com/path</p>
Three things in that text will fight with JSON. The two double quotes around there will each try to end the string. The newline between the paragraphs is a literal control character JSON forbids inside a string. And the </p> sequences are harmless in a plain JSON file but dangerous if this JSON is ever printed inside an HTML <script> tag. Paste the fragment into the JSON Escape / Unescape tool, turn on "wrap in quotes," and you get a single, valid JSON string: the quotes become \", the newline becomes \n, and with "escape forward slash" enabled the closing tags become <\/p>. Drop that whole quoted value straight into your body field and the request goes through.
The reverse trip is just as routine. When a teammate sends you a log line with a JSON-escaped message field full of \n and \", paste it into the tool, switch to Unescape, and read the original multi-line text with the backslashes gone. Because the two operations are exact inverses, you can move a payload out to edit it and back in without fear of corruption - the character-count summary confirms the transformation ran, and any malformed escape is flagged with its position instead of silently mangling the text. That round-trip safety is what turns escaping from a nervous manual step into something you can wire into a workflow and forget about.
Frequently Asked Questions
What does it mean to escape a string for JSON? JSON string values cannot contain a raw double quote, a raw backslash, or raw control characters like newlines and tabs. Escaping replaces each of these with a backslash sequence - " becomes ", a newline becomes \n, a tab becomes \t - so the text can be placed inside a JSON string without breaking the surrounding structure.
How is this different from a JSON formatter? A JSON formatter pretty-prints or minifies a whole JSON document and validates its structure. This tool works one level down, on the contents of a single string: it makes arbitrary text safe to embed inside a JSON value, or decodes an escaped value back to raw text. Use the formatter for documents and this tool for individual strings.
Which characters get escaped? The double quote ("), the backslash (\), backspace (\b), form feed (\f), newline (\n), carriage return (\r), and tab (\t) are always escaped, along with any other control character below U+0020 as \uXXXX. Optionally, the forward slash becomes / and every non-ASCII character becomes \uXXXX.
Should I escape the forward slash? It is optional. The JSON spec allows both / and /, so plain / is valid. The one place / matters is inside an HTML



