What is JSON escaping?
A JSON string is wrapped in double quotes, so some characters can't appear inside it literally. A raw
" would end the string early, and a raw line break isn't allowed at all. Escaping replaces those characters with a backslash sequence, such as \" or \n, so the text can
sit safely inside a JSON document and be parsed back exactly.
You need it whenever you put text inside JSON by hand. Common cases are a multi-line message in an API
request body, a JSON document stored as a string field, a config value in a .env file, or a payload
in a curl -d command.
Characters that must be escaped
| Character | Escaped as | Notes |
|---|---|---|
Double quote " | \" | Required |
Backslash \ | \\ | Required |
| Newline | \n | Required (raw line breaks are invalid) |
| Carriage return | \r | Required |
| Tab | \t | Required |
| Backspace, form feed | \b, \f | Required |
| Other control characters (U+0000–U+001F) | \u0000–\u001f | Required |
Forward slash / | \/ | Optional |
| Any Unicode character | \uXXXX | Optional, for ASCII-only output |
How to use this JSON escaper
- Paste your text or JSON into the left panel, or drop a file onto it.
- The escaped string appears on the right instantly. Click Copy or Download.
- Turn on Wrap in quotes to get a complete string literal, or Escape Unicode for pure-ASCII output, for systems that mangle UTF-8.
- Use ⇄ Swap to feed the result back in and unescape it, which is a quick round-trip check.
-
Share link copies a URL that reopens the tool with your input. The data lives in the URL's
#fragment, which browsers never send to a server.
Example
Escaping this JSON object so it can be stored as a string value:
{
"user": "ada",
"note": "Say \"hi\""
}
{\n \"user\": \"ada\",\n \"note\": \"Say \\\"hi\\\"\"\n}
Each real line break became the two characters \n, the existing \" became
\\\", and every other quote became \".
How to escape JSON in code
Every language's JSON library does this for you. Never escape strings with manual replace() calls,
because it's easy to miss control characters or escape the backslash in the wrong order.
JavaScript / TypeScript
const message = 'She said "hi"\nPath: C:\\temp';
// Escaped string literal, including the surrounding quotes
JSON.stringify(message);
// => "She said \"hi\"\nPath: C:\\temp"
// Embedding user text safely inside a JSON document
const body = JSON.stringify({ message });
Python
import json
message = 'She said "hi"\nPath: C:\\temp'
json.dumps(message)
# '"She said \\"hi\\"\\nPath: C:\\\\temp"'
# Keep non-ASCII characters readable instead of \uXXXX
json.dumps("café ☕", ensure_ascii=False) # '"café ☕"'
Go
b, _ := json.Marshal("She said \"hi\"\n")
fmt.Println(string(b)) // "She said \"hi\"\n"
// json.Marshal also escapes <, > and & as \u003c etc.
// Use an Encoder with SetEscapeHTML(false) to keep them literal.
Java
// Jackson
ObjectMapper mapper = new ObjectMapper();
String escaped = mapper.writeValueAsString("She said \"hi\"\n");
// Gson
String escaped2 = new Gson().toJson("She said \"hi\"\n");
PHP
echo json_encode('She said "hi"' . "\n");
// "She said \"hi\"\n"
// Keep slashes and Unicode readable
echo json_encode($text, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
Common pitfalls
-
Double escaping. Calling
JSON.stringifyon something that is already a JSON string produces"{\"a\":1}", with backslashes everywhere. Parse first, or use the JSON Unescape tool to recover the original. -
Shell quoting is a separate layer. In
curl -d '...', the JSON must be valid and survive the shell. Single-quote the whole body in Bash, and escape single quotes inside it as'\''. -
JSON inside HTML. When inlining JSON in a
<script>tag, escape</to prevent a stray</script>from breaking the page. The Escape slashes option handles this. -
Single quotes don't need escaping.
\'is not a valid JSON escape, and strict parsers will reject it.
Frequently asked questions
What characters need to be escaped in JSON?
Inside a JSON string you must escape the double quote (\"), the backslash (\\) and all control characters U+0000 to U+001F, such as newline (\n), carriage return (\r) and tab (\t). Everything else, including emoji and non-English text, can appear as-is.
Do I need to escape forward slashes in JSON?
No. Escaping / as \/ is allowed but optional. It is only useful when you embed JSON inside an HTML <script> tag, where escaping it prevents a literal "</script>" from closing the tag early.
What is the difference between JSON escape and JSON stringify?
They are the same operation applied to a string. JSON.stringify("text") returns the escaped value wrapped in double quotes. This tool shows the escaped content without quotes by default; enable "Wrap in quotes" to get exactly what JSON.stringify returns.
Why does my JSON have backslashes everywhere?
It has been serialized twice: an object was turned into a JSON string, and that string was serialized again. Switch this tool to Unescape mode with "Auto-detect double escaping" enabled to get the original JSON back.
Is my data sent to a server?
No. Escaping and unescaping run entirely in your browser with JavaScript. Nothing you paste is uploaded or stored, and the page keeps working offline once loaded.