A single trailing comma in a JSON payload can bring down a production API endpoint — and JSON's spec forbids trailing commas entirely.
JSON (JavaScript Object Notation) has become the universal data interchange format. REST APIs, configuration files, database documents, GraphQL responses, webhook payloads — all JSON. But JSON is deceptively strict. JavaScript, the language it was derived from, accepts trailing commas, single quotes, and comments. JSON accepts none of them. The gap between what developers expect and what the spec allows causes a disproportionate share of integration bugs.
This guide covers JSON formatting, validation, common syntax errors, and how to use the free WOWHOW JSON formatter and validator to debug payloads before they hit your API.
Try it yourself: Free JSON Formatter & Validator — free, no signup, runs in your browser.
JSON Syntax: The Complete Rules
JSON has exactly six value types: strings, numbers, objects, arrays, booleans (true/false), and null. Everything else is invalid. Understanding these rules prevents 90% of JSON syntax errors before they occur.
Strings
JSON strings must use double quotes — not single quotes, not backticks, not unquoted identifiers. Any of the following are invalid JSON strings:
// INVALID — single quotes
{ 'name': 'Alice' }
// INVALID — unquoted keys
{ name: "Alice" }
// VALID
{ "name": "Alice" }
Backslash escape sequences are mandatory for: double quotes ("), backslashes (\), newlines, carriage returns, tabs, and Unicode characters. An unescaped newline inside a string is a syntax error.
Numbers
JSON numbers cannot have leading zeros (except for 0 itself), cannot be Infinity, cannot be NaN, and cannot have a trailing decimal point. 1. is invalid; 1.0 is valid. Hexadecimal notation (0xFF) is also invalid — JSON numbers are always decimal.
Objects and Arrays
The most common JSON error is the trailing comma:
// INVALID — trailing comma after last property
{
"name": "Alice",
"age": 30,
}
// VALID
{
"name": "Alice",
"age": 30
}
JavaScript (and many modern JSON parsers in lenient mode) silently ignore trailing commas. The JSON spec does not. If your payload is destined for a strict parser — a Go server, a Java API, a strict browser JSON.parse() — a trailing comma will throw a parse error.
Comments Are Forbidden
JSON does not support comments. Not // line comments, not /* block comments */. If you need commented configuration files, use YAML or TOML instead. In JSON, any // or /* is a syntax error.
Common JSON Errors and How to Fix Them
Unexpected Token Error
This is the most frequent parse error. It usually means a trailing comma, an unquoted key, or a single-quoted string. Paste the payload into the JSON validator and it will point you to the exact line and character position of the error.
Unterminated String
Occurs when a string contains an unescaped double quote or a literal newline. For example:
// INVALID — unescaped quote inside string
{ "message": "She said "hello" to me" }
// VALID — escaped quotes
{ "message": "She said "hello" to me" }
Invalid Escape Sequence
Only a specific set of escape sequences are valid in JSON: double quotes, backslashes, forward slashes, backspace, form feed, newline, carriage return, tab, and Unicode hex sequences. Any other backslash sequence is a syntax error in strict JSON, even though JavaScript regex patterns use them.
Number Precision Issues
JSON does not define a maximum number size, but JavaScript's JSON.parse() uses IEEE 754 double-precision floating point, which cannot accurately represent integers above 253 (9,007,199,254,740,992). Database primary keys and Snowflake IDs often exceed this limit. The standard solution is to serialize large integers as strings in JSON, or use a BigInt-aware parser.
JSON Formatting: Compact vs Pretty-Printed
A valid JSON document can be entirely on one line or spread across many. Compact (minified) JSON reduces payload size — important for high-volume APIs. Pretty-printed JSON is human-readable — important for debugging, logging, and configuration files.
The JSON formatter converts between both forms instantly. Paste minified JSON and get a readable, indented view. Paste multi-line JSON and get a compact payload ready for transmission.
Indentation Conventions
JSON doesn't mandate an indentation style, but 2-space and 4-space indentation are the dominant conventions. Node.js's JSON.stringify() uses 2 spaces by default when given an indent argument (JSON.stringify(obj, null, 2)). Python's json.dumps() uses 4 spaces with indent=4. Most linters and formatters allow configuration.
JSON Schema: Structural Validation Beyond Syntax
Syntax validation checks whether a string is valid JSON. Schema validation checks whether the JSON has the right structure — required fields, correct types, valid enum values, string patterns. JSON Schema (standardized at json-schema.org) is the most widely used schema language for this purpose.
A minimal JSON Schema for a user object looks like:
{
"$schema": "https://json-schema.org/draft/2020-12",
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string", "format": "email" },
"role": { "type": "string", "enum": ["admin", "user", "viewer"] }
},
"required": ["id", "email"]
}
Schema validation is critical for API contracts: before your server processes a webhook payload or API request body, validate it against a schema to catch malformed data before it reaches your business logic.
JSON in 2026: NDJSON, JSON Lines, and Streaming
For large datasets and streaming APIs, standard JSON has a problem: the entire document must be parsed before any value is available. Two formats solve this:
NDJSON (Newline-Delimited JSON): One valid JSON object per line, separated by newlines. Used by log aggregators, bulk API endpoints, and streaming pipelines. Each line can be parsed independently as it arrives.
JSON Lines: Effectively identical to NDJSON — one JSON value per line. The name is used interchangeably in many ecosystems. The Hugging Face datasets library uses JSON Lines (.jsonl) as its primary format.
OpenAI's streaming API, Anthropic's streaming API, and most LLM providers return streaming responses as Server-Sent Events with NDJSON payloads. When debugging a streaming API that seems to return garbled data, the issue is usually that the client is trying to parse the entire streamed response as a single JSON document rather than line by line.
People Also Ask
What is the fastest way to validate JSON online?
Paste your JSON into the WOWHOW JSON formatter — it validates on every keystroke, highlights the exact error position, and shows a formatted preview. No signup required.
Why does my JSON parse in JavaScript but fail in Python?
JavaScript's JSON.parse() in some environments accepts trailing commas and single-quoted strings in lenient mode. Python's json.loads() follows the strict RFC 8259 spec and rejects both. The safest approach is to produce strictly valid JSON from the start — no trailing commas, double quotes only, no comments.
How do I handle large integers in JSON without precision loss?
Serialize large integers as strings ("id": "9007199254740993") and document that the consumer must parse them as BigInt. Some APIs (Twitter/X Snowflake IDs) do this by convention. Alternatively, use a JSON library that supports arbitrary-precision numbers (Python's decimal mode, Java's BigDecimal).
What is the difference between JSON and JSON5?
JSON5 is a superset of JSON that adds trailing commas, single-quoted strings, comments, and hex numbers. It is useful for human-written configuration files. It is not appropriate for machine-to-machine API data exchange — use strict JSON there. Most JSON validators do not accept JSON5 by default.
For more developer productivity tools, browse the full WOWHOW tools catalog — including JWT decoders, regex testers, and base64 encoders.
Originally published at wowhow.cloud
Top comments (0)