How to Escape a JSON String: Quotes, Newlines & Unicode
Learn how to JSON escape a string for safe embedding - handle quotes, backslashes, newlines, tabs, control characters, and optional Unicode escaping.
A JSON string can contain text, but some characters must be escaped first. Quotes, backslashes, line breaks, tabs, and control characters all need special handling. This guide shows how to escape a JSON string online, in JavaScript, and in Python so the result is safe to embed in JSON, code, logs, or database fields.
The quickest way: JSON escape online
Paste raw text into the JSON Escape tool and it will return a JSON-safe string. It escapes quotes, backslashes, newlines, tabs, and control characters, with options for wrapping the output in quotes and escaping non-ASCII characters as \uXXXX.
Because text can contain secrets, prefer a client-side JSON escape tool. Ours runs in your browser and does not upload the input.
Use it when: you need to put raw text safely inside a JSON string.
What JSON escaping does
Inside JSON strings, these characters need special treatment:
"becomes\"\becomes\\- newline becomes
\n - tab becomes
\t - carriage return becomes
\r - control characters become Unicode escapes like
\u0000
For example:
She said "hello"
becomes:
"She said \"hello\""
Without escaping the inner quotes, the JSON parser thinks the string ends early.
Escape for a JSON value vs escape for embedding
There are two common outputs:
Escaped content only:
She said \"hello\"
Full JSON string value:
"She said \"hello\""
If you are pasting into an existing JSON property, you usually want the content only. If you need a complete valid JSON string, wrap it in quotes.
Unicode escaping
JSON supports Unicode text directly, so this is valid:
"cafe"
Some systems prefer ASCII-only output with \uXXXX escapes. That can be useful for old systems, logs, or transport layers that do not handle Unicode cleanly. For modern APIs, direct UTF-8 is usually easier to read.
JSON escape in JavaScript
JavaScript’s JSON.stringify is the safest built-in option:
const raw = 'She said "hello"\nNext line';
const escaped = JSON.stringify(raw);
console.log(escaped);
// "She said \"hello\"\nNext line"
If you need the escaped content without the surrounding quotes, remove the first and last character carefully:
const contentOnly = JSON.stringify(raw).slice(1, -1);
Avoid hand-written replacement chains unless you fully cover backslashes, quotes, control characters, and order of operations.
JSON escape in Python
Python’s json.dumps does the same job:
import json
raw = 'She said "hello"\nNext line'
escaped = json.dumps(raw)
print(escaped)
By default, Python escapes non-ASCII characters. Use ensure_ascii=False if you want readable UTF-8:
json.dumps(raw, ensure_ascii=False)
Common mistakes
The biggest mistake is escaping twice. If your output contains \\n when you expected a real newline escape, the string may have been escaped again. Another common mistake is forgetting to escape backslashes before quotes. That breaks Windows paths and regular expressions.
If the string looks over-escaped, use the JSON Unescape tool to decode it and check what the receiving system will actually read.
Which method should you use?
For one-off text, use the online JSON Escape tool: paste, choose wrapping and Unicode options, and copy the safe string. Use JSON.stringify in JavaScript and json.dumps in Python for code. Whatever you choose, let a real JSON encoder do the escaping instead of editing by hand.
Need the reverse? Read how to unescape JSON strings, or use the JSON Formatter to inspect a full document.
Ready to merge your JSON?
Combine files or snippets in your browser — free and private.
Open the merge toolKeep reading