How to Escape Strings for JSON Without Breaking Syntax
Pasting raw text - code, logs, or quoted dialogue - directly into a JSON string breaks parsing. This guide explains JSON escaping rules and how to escape text correctly in seconds.
Try to paste a chunk of code, a log line with quotation marks, or a multi-line paragraph straight into a JSON string value, and the document breaks. JSON strings have strict rules about which characters are allowed to appear literally and which must be represented as an escape sequence - and violating those rules produces a parse error instead of the payload you intended.
This guide walks through exactly which characters need escaping, why the rules exist, and how to apply them correctly every time. You will also see how the JSON Escape tool handles the conversion instantly in your browser, so you can take raw text - including secrets or proprietary code - and turn it into a safe JSON string without exposing it to a server.
What does escaping a string for JSON mean?
A JSON string is any sequence of characters wrapped in double quotes. But a handful of characters have special meaning inside that wrapper: the double quote itself would prematurely end the string, a backslash starts an escape sequence, and raw newlines or tabs are not permitted to appear literally in strict JSON text. Escaping replaces each of these problem characters with a two-character (or six-character, for Unicode) sequence that represents the original character without breaking the surrounding syntax.
The result looks a little denser than the source text, but it parses back to exactly the same characters. Nothing about the underlying content changes - only its textual representation changes so it can live safely inside a JSON string literal.
Raw text vs. escaped text
Dialogue with quotes and a line break, prepared for a JSON string.
Input
She said "hello"
and left.Output
She said \"hello\"\nand left.Why escaping matters
Most developers do not think about escaping until they hit a parse error they cannot explain. Here is why it deserves attention up front, not just as a bug fix:
- Valid syntax - a single unescaped quote inside a string terminates it early, and everything after becomes a syntax error the parser cannot recover from.
- Predictable round-tripping - text escaped correctly with JSON Escape will always decode back to the exact original bytes with JSON Unescape.
- Safe embedding of arbitrary content - code snippets, SQL, HTML, and log output routinely contain quotes and newlines; escaping lets you drop that content into JSON without hand-editing it.
- Cross-language compatibility - correctly escaped JSON parses the same way in JavaScript, Python, Java, and every other JSON-aware language, since the escape rules are part of the spec, not a language convention.
How JSON string escaping works
The JSON specification defines a small set of characters that must be escaped inside a string, plus a general mechanism for anything else that is not printable. Double quotes become \", backslashes become \\, and the common whitespace controls each get their own short form: newline becomes \n, tab becomes \t, carriage return becomes \r, backspace becomes \b, and form feed becomes \f. Any other control character - and, at your discretion, other characters you want represented explicitly - is written as a \u sequence followed by its four-digit hexadecimal code point.
Forward slashes are the one character people often expect to be escaped but are not required to be. JSON permits a literal / inside a string, so a correct escaper leaves it alone by default - escaping it is optional and mainly a holdover from contexts where JSON is embedded inside HTML <script> tags to avoid an accidental closing tag.
Your text never leaves your browser
Escaping is pure text transformation, so it can - and should - happen entirely on your device. JSON Escape processes everything locally: there is no upload, no server round trip, and no logging. That makes it safe to escape source code, credentials, or customer messages without worrying about where the text ends up.
Step-by-step: escape text for JSON
- Open JSON Escape and paste the raw text you need to embed - a code snippet, a quote-filled sentence, or multi-line log output.
- Let the tool convert quotes, backslashes, newlines, tabs, and other control characters into their escape sequences in one pass.
- Decide whether you want the result wrapped in surrounding double quotes, so it is a ready-to-paste string literal rather than just the inner content.
- Copy the escaped string and paste it directly into the JSON field, request body, or source file that needs it.
There is no configuration to get wrong here - the escape rules are fixed by the JSON spec, so the same input always produces the same, portable output.
Common use cases
- Embedding a code snippet or SQL query inside a JSON string field for an API request.
- Placing multi-line log text into a JSON payload for a bug report or monitoring event.
- Hand-building JSON test fixtures where the source content includes quotes or backslashes.
- Storing a chunk of HTML or markup as a JSON string value in a CMS export.
- Preparing text with embedded quotes for a JSON configuration file that a script will parse.
Common mistakes when escaping JSON strings
Escaping quotes but forgetting backslashes
Because the backslash is the escape character itself, it must be escaped first - as \\ - or every escape sequence after it shifts and the whole string misparses. Manually escaping quotes while leaving existing backslashes untouched is one of the most common ways hand-written JSON strings end up corrupted.
Escaping already-escaped text
If text copied from another JSON document already contains escape sequences and you run it through an escaper again, the backslashes themselves get escaped a second time, producing \\n instead of \n. Always start from the original raw text, not from a string that has already been through this process.
Leaving raw newlines in place
Pasting multi-line text directly into a JSON string without converting the line breaks to \n produces invalid JSON - strict parsers reject literal newline characters inside a string, even though many editors will happily display them.
Assuming forward slashes must be escaped
Escaping every / as \/ is unnecessary for valid JSON and just adds noise. It is only useful in the narrow case of inlining JSON inside an HTML <script> block, and even then it is a style choice rather than a spec requirement.
Best practices for escaping JSON strings
- Escape from the original raw source, never from text that has already passed through an escaper, to avoid double-escaping.
- When the escaped text represents a full JSON document rather than plain text, reach for JSON Stringify instead, since it also validates the document before encoding it.
- Keep a matching JSON Unescape step in mind for the reverse direction, so round-tripping stays lossless.
- For sensitive text - credentials, internal code, customer data - use a tool that runs locally rather than one that uploads your input to a server.
Related tools
Turn a JSON-escaped string back into readable raw text.
Convert a JSON value into an escaped JSON string literal.
Pretty-print JSON with configurable indentation and instant validation.
Encode text or files to Base64, with an optional URL-safe variant.
Related guides
Escaped JSON strings collapse real newlines, quotes, and Unicode characters into cryptic sequences. This guide shows how to reverse that process and read the original text safely.
Sometimes a JSON value has to live inside another JSON field, an env var, or a database column - as text. This guide explains stringifying, common pitfalls like double-encoding, and how to do it privately.
Minified JSON is unreadable and hard to debug. This guide covers how JSON formatting works, when to use it, common mistakes, and how to pretty-print JSON without uploading it anywhere.
Base64 encoding turns arbitrary bytes into safe, portable text. This guide covers how encoding works, UTF-8 pitfalls, URL-safe output, and how to encode text or files without uploading anything.