To escape a JSON string, replace every double quote with \", every backslash with \\, and every newline with \n. Doing this by hand for anything longer than a few words is error-prone — this guide shows the fastest way to do it online for free, explains every required escape sequence, and covers common mistakes like a trailing backslash or an unescaped quote breaking your JSON.
Escape or unescape a JSON string right now — free, private, in your browser:
Open JSON String Escape →What is JSON string escaping?
A JSON string is always wrapped in double quotes: "hello world". The problem is that some characters have special meaning inside those quotes. If your text itself contains a double quote, a backslash, or a newline, the JSON parser can't tell where your string ends and where the surrounding structure begins — so it throws a parse error.
Escaping solves this by replacing those problematic characters with safe two-character sequences that start with a backslash. For example:
Raw text:
He said "hello"
and left.
Escaped (ready to put between JSON quotes):
He said \"hello\"\nand left.
Put that between the double quotes of a JSON field and it is a valid JSON string: "He said \"hello\"\nand left."
Which characters must be escaped?
Per RFC 8259 (the JSON specification), two characters are mandatory to escape, and a whole class of control characters must also be escaped:
| Character | Escaped as | Notes |
|---|---|---|
" Double quote | \" | Mandatory — ends the string prematurely if unescaped |
\ Backslash | \\ | Mandatory — starts an escape sequence if unescaped |
| Newline (LF) | \n | U+000A — mandatory |
| Carriage return | \r | U+000D — mandatory |
| Tab | \t | U+0009 — mandatory |
| Backspace | \b | U+0008 — mandatory |
| Form feed | \f | U+000C — mandatory |
| Other control chars | \uXXXX | U+0000–U+001F — mandatory |
/ Forward slash | \/ | Optional — only needed inside HTML <script> tags |
Every other Unicode character — letters, digits, emoji, non-ASCII scripts — may appear unescaped inside a JSON string.
How to escape a JSON string online (step by step)
- Open the JSON String Escape tool. It loads instantly with no signup required.
- Paste your raw text into the Input field. This can be a SQL query, an error message, a code snippet, or any text containing quotes or backslashes.
- Click Escape. The tool runs
JSON.stringify()internally and strips the surrounding double quotes, giving you the inner escaped string. - Click Copy Output to copy the result to your clipboard, then paste it between the double quotes of your JSON field.
To reverse the process — turning an already-escaped string back into plain text — paste the escaped string and click Unescape instead.
Worked example
Suppose you want to store this SQL query as a JSON string value:
SELECT * FROM users WHERE name = "Alice" AND active = 1;
Paste it into the tool and click Escape. The output:
SELECT * FROM users WHERE name = \"Alice\" AND active = 1;
Now you can safely place this between quotes in your JSON document:
{
"query": "SELECT * FROM users WHERE name = \"Alice\" AND active = 1;"
}
Common use cases
- SQL or code in a JSON field — queries and code snippets almost always contain double quotes and newlines that must be escaped.
- Embedding JSON inside JSON — storing a JSON payload as a string value in another JSON document requires all the inner quotes and braces to be escaped.
- Log lines and error messages — multi-line stack traces need
\nto fit on a single JSON string. - Hardcoding a string in source code — pasting any text that contains quotes into a JSON config or a string literal requires escaping first.
- API payloads — body text from a form, a user comment, or a document that is being sent as a JSON string field.
Escaping in JavaScript, Python, and other languages
You don't always need a tool — every language with a JSON library can escape a string for you:
JavaScript
const raw = 'He said "hello"\nand left.';
// JSON.stringify wraps in quotes — slice removes them
const escaped = JSON.stringify(raw).slice(1, -1);
// → He said \"hello\"\nand left.
Python
import json
raw = 'He said "hello"\nand left.'
# json.dumps wraps in quotes — strip them
escaped = json.dumps(raw)[1:-1]
# → He said \"hello\"\nand left.
Java
import com.fasterxml.jackson.databind.ObjectMapper;
String raw = "He said \"hello\"\nand left.";
ObjectMapper mapper = new ObjectMapper();
// writeValueAsString wraps in quotes — strip them
String escaped = mapper.writeValueAsString(raw);
escaped = escaped.substring(1, escaped.length() - 1);
Using a library is always safer than writing your own replacement loop — libraries handle edge cases like Unicode control characters and surrogate pairs that are easy to miss manually.
Common mistakes
Trailing backslash
If your escaped string ends with a lone \, the JSON parser sees it as the start of an escape sequence and throws an error. This happens when you copy only part of an escaped string. Make sure the final character is not a single backslash.
Unescaped quote inside the string
Pasting text that contains " directly into a JSON value without escaping it causes the parser to think the string ended there. The error message is usually Unexpected token or Expected comma or closing brace.
Real newlines inside a JSON string
A literal line break inside a JSON string is invalid. Multi-line text must use \n (and \r\n on Windows). Many editors silently paste a real newline when you hit Enter inside a JSON value, which then fails validation.
Double-escaping
If you run the escape tool twice on the same string, you get \\\" instead of \" — the backslashes themselves get escaped. Always start from the raw, unescaped text.
JSON string escaping vs URL encoding
These two are often confused because both convert special characters into a safe representation. The difference is the target format:
| Technique | Output format | Used for |
|---|---|---|
| JSON string escaping | \", \\, \n, \uXXXX | Text inside a JSON string value |
| URL encoding (percent-encoding) | %22, %5C, %0A | Parameters inside a URL |
| Base64 encoding | SGVsbG8= | Binary data in text-only fields |
They are not interchangeable. If you need to put text into a URL query string, use the URL Encoder. If you need to encode binary data, use the Base64 Encoder. If you need text inside a JSON string, use the JSON String Escape tool.
Frequently Asked Questions
What does it mean to escape a string in JSON?
Escaping a string means replacing characters that would break a JSON string literal with backslash sequences. A double quote becomes \", a backslash becomes \\, and a newline becomes \n. This lets you safely place arbitrary text inside the double quotes of a JSON string value.
Which characters must be escaped in a JSON string?
Per RFC 8259, the double quote ("), the backslash (\), and all control characters (U+0000 to U+001F — newline, tab, carriage return, etc.) must be escaped. All other Unicode characters may appear unescaped.
What is the difference between escape and unescape?
Escape converts raw text into a JSON-safe form by adding backslash sequences, so you can paste it between the quotes of a JSON value. Unescape does the reverse: it takes an already-escaped string and turns the sequences back into the original characters.
Does the forward slash need to be escaped in JSON?
No. The forward slash (/) may be escaped as \/ but does not need to be. It is sometimes escaped when JSON is embedded inside an HTML <script> tag to avoid the sequence </script>, but plain / is perfectly valid JSON.
How is JSON string escaping different from URL encoding?
JSON string escaping uses backslash sequences (\", \n, \uXXXX) so text fits inside a JSON string. URL encoding (percent-encoding) replaces unsafe characters with %XX hex codes so text fits inside a URL. They solve the same problem for different formats and are not interchangeable.
Is my text sent to a server when I use the escape tool?
No. The JSON String Escape tool runs JSON.stringify() and JSON.parse() directly in your browser. Your text never leaves your device, which makes it safe for tokens, passwords, SQL queries, and other sensitive content.
Escape or unescape a JSON string now — free and private
Paste any text, click Escape, and get a string ready to paste between JSON quotes. Nothing is uploaded — it all runs in your browser.
Need to escape a JSON string right now?
Open JSON String Escape →