What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format. Originally derived from JavaScript, it has become the universal standard for data exchange between web services, APIs, and applications. JSON's simplicity — built from just objects, arrays, strings, numbers, booleans, and null — makes it both human-readable and machine-parseable.
Most modern APIs return JSON by default, and configuration files across every major platform (package.json, tsconfig.json, composer.json) use it as their format of choice. Understanding how to read, write, and validate JSON is a fundamental skill for any developer.
Common JSON errors and how to fix them
Trailing commas are the number one cause of invalid JSON. JavaScript allows a comma after the last item in an array or object ([1, 2, 3,]), but JSON does not. Remove the final comma before any closing bracket or brace.
Single quotes instead of double quotes are another frequent mistake. JSON requires double quotes for both property names and string values. {'name': 'value'} is valid JavaScript but invalid JSON — it must be {"name": "value"}.
Unquoted property names trip up developers coming from JavaScript, where { name: "value" } is perfectly valid. In JSON, every key must be a double-quoted string.
Comments are not allowed in standard JSON. If you paste code from a JSONC file (like VS Code's settings.json), strip the comments first. For files that need comments, consider JSON5 or YAML.
Missing commas between properties often happen when copying and pasting. Each key-value pair (except the last) must end with a comma. The error message will typically point to the line after the missing comma.
Invalid values like undefined, NaN, Infinity, and function references are valid in JavaScript but not in JSON. Replace them with null, a number, or remove them entirely.
JSON formatting best practices
Indentation is a matter of convention, but 2-space indentation is the most common choice in the JavaScript ecosystem. Node.js, npm, and most linters default to 2 spaces. Some teams prefer 4 spaces (common in Python ecosystems) or tabs. The important thing is consistency — pick one and enforce it with a formatter.
Minify for production. If you're sending JSON in API responses, minified JSON (no whitespace) reduces payload size and transfer time. Most web frameworks do this automatically, but you can also pipe through JSON.stringify(data) without the spacing arguments.
Prettify for readability. When debugging, editing config files, or reviewing diffs in version control, formatted JSON with proper indentation makes structure visible at a glance. Git diffs in particular benefit enormously from one-property-per-line formatting.