What Escaping a JSON String Means
JSON strings are delimited by double quotes and follow strict rules about which characters may appear literally inside them. Any character that would break the string (like an unescaped quote) or corrupt parsers (like a raw newline in a single-line context) must be replaced with an escape sequence — a backslash followed by a specific character.
Escaping is what turns arbitrary text into a valid JSON string literal. If you have a raw string with quotes, newlines, or backslashes and want to embed it inside a JSON payload — for example, sending a multi-line message as the value of a "body" field in an API POST — the entire string must be escaped first. Otherwise the JSON parser fails immediately.
The Six Required JSON Escape Sequences
The JSON spec (RFC 8259) requires these characters to be escaped inside strings: " (double quote → \"), \ (backslash → \\), and all control characters below U+0020. Control characters have short escapes: \b (backspace), \f (form feed), \n (newline), \r (carriage return), \t (tab).
Any other character can optionally be escaped as \uXXXX where XXXX is the four-hex-digit unicode code point. The forward slash / is not required to be escaped but is allowed to be escaped as \/ — this is a quirk of the spec that exists so JSON can be safely embedded in </script> tags.
Escaping JSON Strings in Code
JavaScript
// JSON.stringify escapes everything correctly, then remove outer quotes
const raw = 'Hello "World"\nLine 2';
const escaped = JSON.stringify(raw).slice(1, -1);
console.log(escaped);
// Hello \"World\"\nLine 2
// To embed inside a larger JSON string:
const payload = `{"message": "${escaped}"}`;
// Or just build the object and stringify it — much safer:
const safe = JSON.stringify({ message: raw });Python
import json
raw = 'Hello "World"\nLine 2\tTabbed'
# json.dumps of a string returns a valid JSON string literal with quotes
quoted = json.dumps(raw)
print(quoted)
# "Hello \"World\"\nLine 2\tTabbed"
# Strip the outer quotes if you just need the escaped content
escaped_only = quoted[1:-1]
# ensure_ascii=False keeps Unicode characters literal (é not \u00e9)
print(json.dumps(raw, ensure_ascii=False))Command Line
# jq can escape a raw string safely — @json filter
raw_text=$(cat message.txt)
echo "$raw_text" | jq -Rs @json
# For a curl body: use jq to build the whole payload
jq -n --arg msg "$(cat message.txt)" '{message: $msg}' | curl -d @- https://api.example.com/send
# The --arg flag correctly escapes the shell string for JSON — no manual escaping neededEscape vs Encode — Not the Same Thing
JSON escaping and URL encoding (percent-encoding) are frequently confused. They solve different problems. URL encoding converts special characters to %HH hex sequences so a string can safely appear in a URL query parameter — hello world becomes hello%20world. JSON escaping converts characters so a string can safely appear inside a JSON string literal — hello "world" becomes hello \"world\".
You often need both. A JSON payload containing a URL as a value must have the URL either fully unescaped (parsed by JSON as a normal string) or URL-encoded first if the URL itself contains characters like spaces. The URL is not JSON-escaped inside the JSON string — JSON handles that automatically at serialization time.
Common Escaping Mistakes
1. Double-escaping. Feeding already-escaped JSON back through an escaper produces things like \\n instead of \n. Always escape once, at serialization boundaries. If you find yourself calling JSON.stringify on a string that already contains escape sequences, you probably want to JSON.parse it first.
2. Escaping control characters manually and getting it wrong. Backspace is \b not \backspace. Form feed is \f not \ff. Vertical tab (\v) is not a JSON escape — use \u000b instead. Let a library handle this; do not write escaping by hand.
3. Forgetting that HTML entities are not JSON escapes. If your raw text came from an HTML page and contains &, JSON escaping will not convert it back to &. You need to HTML-decode first, then JSON-escape.
Related JSON Tools
- JSON Formatter (Parent Tool) — the underlying formatter used by every variant
- Format JSON Online — general-purpose beautifier with 2/4-space and tab options
- Minify JSON Online — strip whitespace for production payloads
- JSON Validator — validate without formatting
- JSON Diff Checker — compare two JSON objects side-by-side