The JSON Escape & Encoding Cheat Sheet
The string escapes that actually exist, the numbers that lie to you, the invisible characters that break parsers, and the real differences between JSON, JSON5, JSONC and NDJSON. Every tool recommended below is 100% client-side — nothing you paste ever leaves your browser.
1 · The complete JSON escape table
JSON strings allow exactly eight escape forms. Anything else starting with \ is invalid — a surprisingly common cause of validator errors after hand-editing config files.
| Escape | Character | Notes |
|---|---|---|
\" | Double quote U+0022 | Mandatory — a bare " ends the string |
\\ | Backslash U+005C | Mandatory — a bare \ starts an escape |
\/ | Solidus / | Legal but optional; mostly a habit from embedding JSON in <script> tags |
\b | Backspace U+0008 | Rarely seen in the wild |
\f | Form feed U+000C | Shows up in legacy mainstream-format exports |
\n | Line feed U+000A | Real newlines inside a string are illegal — use this |
\r | Carriage return U+000D | Windows line endings inside strings must be escaped |
\t | Tab U+0009 | Pasting a literal tab from a terminal breaks strict parsers |
\uXXXX | Unicode code unit | Four hex digits, BMP only — see surrogate pairs below |
\uXXXX mode) and JSON unescape (which validates the result instead of silently returning garbage).2 · Unicode: surrogate pairs and invisible characters
- Astral characters need two escapes.
\uXXXXcovers one UTF-16 code unit, so 𝄞 (U+1D11E) is\uD834\uDD1E— a surrogate pair. A lone surrogate such as\uD834alone is invalid JSON and some parsers accept it silently, only to crash later. - The PDF/Word copy trap. Documents convert hyphens and quotes to typographic ones (
U+2010,U+201C) and spaces to\u00A0— all perfectly printable but structurally fatal when pasted as code. They're invisible in most editors; a tree viewer that shows raw escapes makes them obvious (try the JSON viewer). - U+2028 / U+2029. These line/paragraph separators are valid inside JSON strings, but before ES2019 JavaScript source code rejected them — old bundlers and templates can still choke on logs containing them.
- The BOM. RFC 8259 says producers MUST NOT add a UTF-8 BOM and parsers MAY ignore it — so plenty of parsers don't. A file that "looks fine" in an editor but fails with
Unexpected token is almost always a BOM. Re-save without it.
3 · Number traps: where JSON silently loses data
| Pattern | What happens | Safe practice |
|---|---|---|
0.1 + 0.2 != 0.3 | IEEE-754 doubles; money, coordinates and rates drift | Serialize monetary values as strings, or round at the boundary |
Integers above 2^53 - 1 (9007199254740991) | 9007199254740993 parses to ...992 in every JS runtime — snowflake IDs, transaction IDs | Ship big IDs as strings; the validator flags lossy integers before they reach your code |
NaN, Infinity, -Infinity | Not JSON. JSON.stringify quietly emits null for them | Use null explicitly, or a sentinel string |
0x1F, 017, +5, .5, 5. | All invalid — no hex, no octal, no leading +, no bare fraction | Normal decimal form only |
1e999 | Grammatically legal, unrepresentable — JS evaluates it to Infinity | Clamp at the producer |
4 · The JSON-in-JSON double-escape
The most confusing sight in debugging: a value that looks like "{\"a\": 1}". That's a JSON document serialized as a string inside another JSON document — logs, webhooks and message queues do this constantly. Strip one level of escaping and re-parse. Two signs you're looking at it: backslashes before every quote, and a validator that parses the envelope fine but the payload is one opaque string. The unescape tool performs exactly this peeling and validates what's underneath; for nested payloads use jq in the browser (.payload | fromjson) as described in running real jq client-side.
5 · JSON vs JSON5 vs JSONC vs NDJSON
Four "JSON" formats with different strictness. Picking the wrong validator wastes hours — the errors read as nonsense when you check one dialect against another's grammar.
| JSON (RFC 8259) | JSON5 | JSONC | NDJSON | |
|---|---|---|---|---|
| Comments | ✗ | ✓ // and /* */ | ✓ | ✗ |
| Unquoted keys | ✗ | ✓ | ✗ | ✗ |
| Single-quoted strings | ✗ | ✓ | ✗ | ✗ |
| Trailing commas | ✗ | ✓ | ✗ | ✗ |
| Multiple documents | ✗ | ✗ | ✗ | ✓ one per line |
| Typical home | APIs, data exchange | human-written config | VS Code / TS configs | log streams, bulk imports |
- Validate the config dialects with the JSON5 formatter and JSONC validator — strict-mode tools will flag their comments as errors, and that's correct behavior.
- NDJSON (newline-delimited) fails with "Unexpected token {" at line 2+ when pasted into a single-document parser. The NDJSON viewer reports per-line counts and errors — see why line-level errors matter.
- Not sure whether your use case even is JSON? The plain-English primer lives at What is JSON, and the config-format comparison at JSON vs YAML vs TOML.
6 · Base64 inside JSON
Binary payloads (images, crypto, PDFs) travel inside JSON as base64 strings. Three things bite people:
- Two alphabets. Standard base64 uses
+and/; the URL-safe variant (JWTs, query params) uses-and_and usually drops the=padding. Mixing them fails decode with a confusing "invalid character". - Size. Base64 inflates payloads by ~33% — for large blobs it's often the reason an API suddenly hits a body-size limit. Minify the JSON envelope first with the minifier (it reports real gzip savings, not just byte counts).
- Padding and MIME newlines. Pasted PEM-style blocks contain line breaks that strict decoders reject. A decoder that tolerates both is described in base64 decoder robustness; round-trip either direction with JSON→Base64 and Base64→JSON.
7 · Schemas: catching all of the above before runtime
Escape tables and number ranges are exactly the kind of contracts a JSON Schema expresses: pattern for strings, maximum / multipleOf for numbers, format for base64. Generate a first draft from a sample with the schema generator, then validate incoming payloads with the AJV-based validator — the workflow is walked through in how to validate JSON against a schema and generating draft-2020-12 schemas from examples.
Why every tool here is browser-only: the fastest way to leak API keys and PII is pasting real payloads into a web tool that processes them server-side. Everything linked on this page parses in your browser tab — the privacy model is explained in how jsonformatter.app keeps data local, and the FAQ covers the edge cases in jsonformatter.app FAQ.