The Complete Guide to JSON Formatting, Validation, and Debugging
Learn everything about JSON: syntax rules, common errors, formatting best practices, validation, and debugging techniques for developers.
JSON (JavaScript Object Notation) is the most widely used data interchange format on the web. Every REST API speaks JSON. Configuration files for tools from ESLint to Docker use JSON. Package managers like npm store dependencies in JSON. If you write software, you work with JSON daily.
This guide covers everything you need to know: the syntax rules, the most common errors developers make, formatting and validation best practices, and practical debugging techniques.
What Is JSON?
JSON is a lightweight, text-based data format that represents structured data using two fundamental structures:
- Objects: Unordered collections of key-value pairs, written as
{"key": "value"} - Arrays: Ordered lists of values, written as
[1, 2, 3]
These two structures can be nested to represent any data shape. JSON supports six value types:
- String:
"hello world"(always double-quoted) - Number:
42,3.14,-1,1.5e10(no leading zeros, no hex) - Boolean:
trueorfalse(lowercase only) - Null:
null(lowercase only) - Object:
{"name": "Alice"} - Array:
[1, "two", true]
JSON was specified by Douglas Crockford in the early 2000s as a subset of JavaScript. It has since become language-independent, with parsers available in every major programming language.
JSON Syntax Rules
Understanding JSON syntax rules prevents the vast majority of parsing errors. Here are the rules that trip developers up most frequently.
Keys Must Be Double-Quoted Strings
{
"name": "Alice",
"age": 30
}
This is the most common source of confusion for JavaScript developers, because JavaScript object literals allow unquoted keys and single-quoted strings. JSON does not. Every key must be a double-quoted string. Every string value must also use double quotes — single quotes are not valid JSON.
No Trailing Commas
{
"name": "Alice",
"age": 30
}
The last property in an object and the last element in an array must not have a trailing comma. This is by far the most common JSON syntax error. JavaScript and TypeScript allow trailing commas, so copying object literals directly into JSON files causes this error constantly.
No Comments
JSON does not support comments of any kind — no //, no /* */, no #. This is a deliberate design choice to keep the format simple and unambiguous. If you need comments in configuration files, consider using JSON5 (a superset that adds comments) or YAML.
Workarounds developers use:
- A
_commentkey:"_comment": "This configures the retry logic" - A separate documentation file
- Switching to YAML or TOML for configuration that needs comments
No Undefined or NaN
JavaScript’s undefined, NaN, Infinity, and -Infinity are not valid JSON values. When serializing JavaScript objects, these values are either omitted (undefined) or cause errors (NaN, Infinity). Be careful with JSON.stringify() — it silently drops properties with undefined values and throws on circular references.
Numbers Cannot Have Leading Zeros
042 is not valid JSON. It must be 42. Similarly, .5 is not valid — it must be 0.5. JSON numbers follow strict formatting rules: an optional minus sign, followed by digits (no leading zeros unless the number is zero), an optional decimal point with digits, and an optional exponent.
Common JSON Errors and How to Fix Them
Error: Unexpected Token
This typically means a syntax violation. Common causes:
- Single quotes instead of double quotes
- Trailing comma after the last property
- Unquoted keys
- Missing comma between properties
Use a JSON Formatter and Validator to pinpoint the exact location. Good validators show the line number and position of the first error.
Error: Unexpected End of JSON Input
This means the JSON string is truncated. The parser reached the end of the input before finding a closing } or ]. Common causes:
- Response body was cut off during network transmission
- String was split incorrectly (e.g., substring that breaks a JSON structure)
- Missing closing brace or bracket
Count your opening and closing braces. Every { needs a matching }, and every [ needs a matching ].
Error: Duplicate Keys
The JSON specification says behavior for duplicate keys is undefined — different parsers handle it differently. Some keep the first value, some keep the last, and some throw an error. Best practice: never use duplicate keys. Lint your JSON to catch duplicates before they cause inconsistent behavior.
Error: Invalid Unicode Escape
JSON supports Unicode escape sequences in the form \uXXXX where X is a hexadecimal digit. Invalid escape sequences (like \x41 which works in JavaScript but not JSON) cause parsing errors. The only valid escape sequences in JSON strings are: \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX.
JSON Formatting Best Practices
Indentation Style
Two-space indentation is the most common convention for JSON files. It keeps the structure visible without consuming too much horizontal space:
{
"name": "project-alpha",
"version": "2.1.0",
"dependencies": {
"express": "^4.18.0",
"lodash": "^4.17.21"
}
}
Four-space indentation is used in some ecosystems (notably Python’s json.dumps default). Tabs are technically valid but rare in JSON. Choose one style and be consistent across your project.
Minification for Production
Minified JSON removes all whitespace that is not inside string values:
{"name":"project-alpha","version":"2.1.0","dependencies":{"express":"^4.18.0","lodash":"^4.17.21"}}
Minification reduces file size by 20-40% depending on the depth of nesting. Use minified JSON for:
- API responses (especially high-traffic endpoints)
- Bundled configuration files
- Data stored in databases or caches
Key Ordering
While JSON objects are technically unordered, sorting keys alphabetically makes JSON files easier to diff in version control:
{
"author": "Alice",
"description": "A sample project",
"name": "project-alpha",
"version": "2.1.0"
}
Many formatters offer a “sort keys” option. Use it for configuration files that are committed to Git.
Validating JSON
Validation goes beyond syntax checking. There are three levels of JSON validation:
1. Syntax Validation
Does the string parse as valid JSON? This catches missing quotes, trailing commas, and structural errors. Use JSON.parse() in JavaScript or a JSON Validator tool.
try {
const data = JSON.parse(jsonString)
} catch (error) {
console.error('Invalid JSON:', error.message)
}
2. Schema Validation
Does the JSON conform to an expected structure? JSON Schema lets you define the shape of your data — required fields, data types, value constraints, and nested structures. Tools like Ajv (Another JSON Schema Validator) enforce schemas at runtime.
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
}
}
3. Business Logic Validation
Does the data make sense in context? This is application-specific: checking that referenced IDs exist, date ranges are valid, and values are within expected bounds. No generic tool handles this — it requires application code.
Debugging JSON Issues
Pretty-Print API Responses
When debugging API responses, the first step is always to format the JSON. Minified JSON is unreadable to humans. Paste the response into a JSON Formatter to see the structure clearly.
In command-line environments, pipe through jq:
curl -s https://api.example.com/data | jq .
Compare JSON Structures
When an API response is not what you expected, compare it against a known-good response. Use a Diff Checker to highlight structural differences. Format both JSON documents with sorted keys first for an accurate comparison.
Inspect Nested Structures
Deeply nested JSON is hard to navigate. Use dot notation mentally to trace paths: response.data.users[0].address.city. When debugging, extract the relevant nested portion and format it separately.
Handle Large JSON Files
Large JSON files (megabytes or more) can crash browser-based tools and slow down text editors. Strategies for large files:
- Use streaming parsers (like
JSONStreamin Node.js) instead ofJSON.parse() - Extract a subset of the data for analysis
- Use command-line tools like
jqfor filtering and transformation
JSON vs. Other Formats
JSON vs. YAML
YAML is a superset of JSON with a cleaner syntax for humans: no quotes around keys, no braces, and support for comments. YAML is preferred for configuration files (Kubernetes, Docker Compose, GitHub Actions). JSON is preferred for data interchange (APIs, databases) because it is unambiguous and parsed identically everywhere.
JSON vs. XML
XML predates JSON and supports attributes, namespaces, schemas (XSD), and transformation (XSLT). JSON is simpler, smaller, and natively supported in JavaScript. Use XML when you need document markup or when integrating with systems that require it (SOAP, RSS, SVG). Use JSON for everything else.
JSON vs. Protocol Buffers
Protocol Buffers (protobuf) are Google’s binary serialization format. They are faster to parse and smaller on the wire than JSON, but they are not human-readable. Use protobuf for high-performance internal services. Use JSON for public APIs and debugging.
Practical Tips
- Always validate JSON from external sources before parsing. Never trust that input is well-formed.
- Use consistent formatting across your project. Configure your editor and formatters to use the same style.
- Sort keys in committed JSON files so version control diffs are meaningful.
- Minify JSON in production API responses to reduce bandwidth and improve performance.
- Bookmark a JSON formatter for instant access when debugging. The ToolPrime JSON Formatter works entirely in your browser with no signup.
- Learn jq basics for command-line JSON processing. Even knowing
jq .(pretty-print) andjq '.key'(extract value) saves significant time. - Use TypeScript interfaces to define JSON structures in your code. This catches structural mismatches at compile time rather than runtime.
JSON’s simplicity is its greatest strength. Six data types, two collection types, and a handful of syntax rules — that is the entire specification. Master these fundamentals and you will spend less time fighting with data and more time building features.