JSON Escape Characters: Complete Reference Guide
Every valid JSON escape sequence explained with examples, common errors, and debugging tips.
Why Escape Characters Exist
JSON strings are delimited by double quotes, which means certain characters inside a string value would prematurely terminate it if written literally. Escape sequences solve this by representing special characters with a backslash followed by a specific character or code. Without escaping, a string containing a quote mark would break the parser.
Escape sequences also represent characters that are invisible or awkward in text: newlines, tabs, backspaces, and arbitrary Unicode code points. JSON is a text format, so it needs a way to embed any character in a string value using only printable ASCII in the escape syntax.
When your JSON fails to parse and the error points inside a string value, an invalid escape is often the cause. Paste the problematic JSON into the JSON Validator to get the exact error location, then consult this guide to fix the escape sequence.
Standard JSON Escape Sequences
JSON defines these escape sequences inside strings: `\"` for double quote, `\\` for backslash, `\/` for forward slash, `\b` for backspace, `\f` for form feed, `\n` for newline, `\r` for carriage return, and `\t` for tab. Any other character following a backslash is invalid in standard JSON.
The forward slash escape `\/` is optional — `/` can appear unescaped in strings. It exists for compatibility with HTML embedding where `</script>` in a JSON string could prematurely close a script tag. Most parsers accept both escaped and unescaped forward slashes.
Example: `{"message": "Line 1\nLine 2\tIndented"}` represents a string with a newline and a tab. Format this with the JSON Formatter to see the structure, or view it in the JSON Viewer to see the parsed string value with actual whitespace characters.
Unicode Escape Sequences
The `\uXXXX` escape represents a single Unicode code point as four hexadecimal digits. `\u0041` is the letter A, `\u00E9` is é, and `\u20AC` is the Euro sign (€). This lets JSON files remain pure ASCII while representing any Unicode character.
For characters outside the Basic Multilingual Plane (code points above U+FFFF), JSON uses surrogate pairs: two `\uXXXX` escapes that together encode one character. Emoji like 😀 (U+1F600) appear as `😀` in escaped form. Modern parsers handle surrogate pairs transparently.
UTF-8 encoding is the recommended default for JSON files. You can include Unicode characters directly in strings without escaping: `{"city": "São Paulo"}` is valid UTF-8 JSON. Use `\u` escapes when you need ASCII-only files or when generating JSON programmatically from binary data.
Common Escape Errors
Single backslashes in file paths break JSON: `{"path": "C:\Users\name"}` is invalid because `\U` is not a valid escape. Correct form: `{"path": "C:\\Users\\name"}` — each backslash must be doubled. This is the most common escape error in configuration files on Windows.
Unescaped newlines inside strings are forbidden. You cannot write a multi-line string value with actual line breaks inside the quotes. Use `\n` for each line break: `{"text": "First line\nSecond line"}`. Alternatively, use an array of strings if each line is a separate element.
Invalid `\u` sequences cause parse failures. `\uZZZZ` (non-hex digits) and `\u123` (too few digits) are both errors. Exactly four hexadecimal digits are required. The JSON Validator reports the character position of invalid escapes.
Escaping in Different Languages
When generating JSON programmatically, never build strings with manual concatenation — use your language's JSON serializer. `JSON.stringify()` in JavaScript, `json.dumps()` in Python, and `json.Marshal()` in Go handle all escaping automatically.
If you must escape manually (e.g., embedding JSON inside a SQL string or shell command), use language-specific escape functions. JavaScript's `JSON.stringify("hello\nworld")` returns `"hello\nworld"` with proper escaping. Python's `json.dumps()` does the same.
Double-encoding is a subtle trap. If a JSON string value contains already-escaped text and you escape it again, `\n` becomes `\\n` — a literal backslash followed by the letter n, not a newline. Paste suspect strings into the JSON Parser to see the actual parsed value.
Escaping JSON Inside Other Formats
Embedding JSON inside HTML requires careful escaping. Inside a `<script>` tag, escape `<` as `\u003c` to prevent `</script>` from closing the tag. Inside HTML attributes, use HTML entity encoding in addition to JSON escaping. The safest approach is to use `json.dumps()` and place the result in a non-executable context.
Embedding JSON in SQL strings requires SQL escaping (single quotes doubled) in addition to JSON escaping. Use parameterized queries instead of string interpolation to avoid both SQL injection and escaping headaches.
Base64 encoding is an alternative when you need to embed binary data or complex escaped strings. Encode the content with Base64 Encode, store the Base64 string as a JSON value, and decode on the receiving end. This eliminates escape sequence issues entirely at the cost of increased size.
Debugging Escape Issues
When JSON fails to parse with an error inside a string, copy the string value in isolation and test it. Wrap it in quotes and pass it through `JSON.parse()` in a browser console or the JSON Validator. This isolates the escape problem from the rest of the document.
For JSON from external sources (APIs, log files, databases), check whether the string has been double-encoded. A value that should contain a newline but displays `\n` literally (two characters) has been escaped twice. Parse once to unescape, inspect the result, and re-serialize correctly.
Validate your fixed JSON with the JSON Validator, format it with the JSON Formatter for readability, and explore the result in the JSON Viewer. For broader validation context, see How to Validate JSON and What is JSON?.
Related Tools
Free online JSON formatter and validator. Beautify, minify, and validate JSON instantly in your browser.
Validate JSON syntax online with precise line and column error messages. Free, fast, and private.
Parse and inspect JSON structure instantly in your browser. Free online JSON parser.
Encode text or files to Base64 format online. Free Base64 encoder with UTF-8 support.
Related Articles
What is JSON? Definition, Syntax, and Examples
Understand what JSON is, how it works, and why it became the default format for APIs and configuration.
How to Validate JSON: Tools and Techniques
Step-by-step guide to validating JSON syntax, catching errors, and fixing common mistakes.
Remove Duplicate JSON Keys: Detection and Prevention
Understand how parsers handle duplicate keys, detect them in your data, and prevent subtle bugs.