JSON Guide: Syntax, Formatting & Common Errors Explained (2026)
Quick Answer
- *JSON is a text-based data format built on six types: string, number, boolean, null, object, and array.
- *92% of developers use REST APIs, which almost universally use JSON — making it the most important data format to know.
- *The three most common parse errors: trailing commas, single quotes, and unquoted keys.
- *JSON does not support comments. Use JSONC or JSON5 for config files that need them.
What Is JSON?
JSON — JavaScript Object Notation — is a lightweight, human-readable text format for storing and exchanging structured data. Douglas Crockford formalized it in 2001, and it became an ECMA standard (ECMA-404) in 2013. The RFC 8259 specification, published in 2017, is the definitive reference.
Despite the "JavaScript" in the name, JSON is language-agnostic. Every major programming language has a built-in JSON parser. Python has json.loads(), Ruby has JSON.parse(), Go has encoding/json, and so on.
According to Postman's 2024 State of the API Report, 92% of developers use REST APIs, which almost universally transmit data as JSON. According to W3Techs (2026), JSON is used in some form on approximately 79% of all websites with detectable data formats. If you work with any API, config file, or web service, you will encounter JSON constantly.
JSON Syntax Rules
JSON has a small, strict grammar. There are six rules that cover almost everything:
- Data is in key-value pairs. Keys must be strings in double quotes. Values can be any JSON type.
- Objects use curly braces.
{"name": "Alice", "age": 30} - Arrays use square brackets.
[1, 2, 3]or["a", "b", "c"] - Strings must use double quotes. Single quotes are not valid JSON.
- No trailing commas. The last item in an object or array must not have a comma after it.
- No comments. The JSON spec explicitly forbids them.
That is the entire spec for most practical purposes. The strictness is intentional — it makes JSON trivially parseable by machines while remaining readable by humans.
JSON Data Types
JSON supports exactly six data types. No more. Understanding each one prevents most beginner mistakes.
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Must use double quotes |
| Number | 42 or 3.14 | No quotes; integers or decimals |
| Boolean | true or false | Lowercase only |
| Null | null | Lowercase only; represents "no value" |
| Object | {"key": "value"} | Unordered key-value pairs |
| Array | [1, 2, 3] | Ordered list; can mix types |
A few things that are not valid JSON values: undefined, NaN, Infinity, functions, dates (use ISO 8601 strings like "2026-03-28" instead), and regular expressions. If you need to represent these, convert them to strings or numbers before serializing.
Common JSON Errors and How to Fix Them
The JSON specification (RFC 8259, 2017) explicitly forbids trailing commas, comments, and single-quoted strings — the three most common sources of JSON parse errors. Here is a reference table:
| Error | Bad Example | Fixed |
|---|---|---|
| Trailing comma | {"a":1,} | {"a":1} |
| Single quotes | {'key':'val'} | {"key":"val"} |
| Unquoted key | {key: "val"} | {"key": "val"} |
| Comments | // comment | (Remove them) |
| Undefined / NaN | undefined | Use null instead |
When a JSON parser throws an error, the message usually includes a line number and character position. Start there. The most common fix is removing a trailing comma or switching single quotes to double quotes. Paste your JSON into our JSON Formatter to pinpoint errors instantly.
JSON vs XML
Before JSON dominated the web, XML was the standard for data interchange. Both formats represent structured data as text, but JSON has largely displaced XML for APIs.
| JSON | XML | |
|---|---|---|
| Readability | Minimal syntax, easy to scan | Verbose tags around every value |
| Size | Compact | Larger (repeated tag names) |
| Data types | 6 native types | Everything is a string; types via schema |
| Comments | Not supported | Supported |
| Array support | Native | Requires conventions |
| Browser parsing | Native (JSON.parse) | Requires DOMParser or library |
| Common use | REST APIs, config, NoSQL | SOAP, RSS/Atom, SVG, HTML |
XML still dominates specific domains: RSS/Atom feeds, SOAP web services, SVG graphics, and Microsoft Office file formats. For new REST APIs, JSON is the default choice. According to Stack Overflow's 2024 Developer Survey, JavaScript (which natively parses JSON via JSON.parse()) is the most-used programming language for the 12th consecutive year, used by 62.3% of professional developers.
Practical JSON Examples
API Response
This is a typical response from a user profile API endpoint:
{
"id": 1042,
"username": "jsmith",
"email": "[email protected]",
"verified": true,
"plan": "pro",
"created_at": "2025-01-15T09:30:00Z",
"preferences": {
"theme": "dark",
"notifications": true
},
"tags": ["developer", "beta-tester"]
}Notice how the object nests another object (preferences) and an array (tags). JSON handles arbitrary nesting with no additional syntax.
Config File
Many tools use JSON for configuration. This is a package.json-style config:
{
"name": "my-app",
"version": "2.1.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"react": "^19.0.0",
"next": "^16.0.0"
},
"engines": {
"node": ">=22.0.0"
}
}Config files tend to be hand-edited, which is exactly where trailing commas and comments sneak in. Tools like VS Code support JSONC (JSON with Comments) for config files, but standard JSON parsers will reject those constructs.
How to Pretty-Print and Minify JSON
Pretty-Printing (Formatting)
Pretty-printing adds whitespace and indentation to make JSON readable. The raw data does not change — only the presentation.
JavaScript:
JSON.stringify(data, null, 2) // 2-space indent
JSON.stringify(data, null, 4) // 4-space indentPython command line:
python3 -m json.tool input.jsonNode.js command line:
cat input.json | node -e "const d=require('fs').readFileSync('/dev/stdin','utf8'); console.log(JSON.stringify(JSON.parse(d),null,2))"Or skip the command line entirely — paste your JSON into our JSON Formatter and click Format.
Minifying
Minifying strips all unnecessary whitespace from JSON to reduce file size. Useful before serving JSON over a network or embedding it in source code.
JavaScript:
JSON.stringify(data) // no indent argument = minifiedPython command line:
python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin),separators=(',',':')))" < input.jsonA minified version of the API response example above would collapse to a single line — hard to read but faster to transmit and parse.
Working with JSON in JavaScript
JavaScript has two native methods for JSON:
JSON.parse(string)— converts a JSON string into a JavaScript object. Throws aSyntaxErrorif the input is invalid JSON.JSON.stringify(value)— converts a JavaScript value into a JSON string. Silently dropsundefinedvalues and functions.
// Parsing JSON from an API
const response = await fetch('/api/user');
const data = await response.json(); // calls JSON.parse internally
// Serializing to JSON
const payload = JSON.stringify({ name: "Alice", active: true });
// Result: '{"name":"Alice","active":true}'One subtle difference: JSON.stringify() will serialize a JavaScript Date object as an ISO 8601 string, but JSON.parse() will not automatically convert that string back into a Date. You have to handle that conversion yourself.
Validate, format, or minify your JSON
Use our free JSON Formatter →Frequently Asked Questions
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format formalized by Douglas Crockford in 2001 and standardized as ECMA-404 in 2013. It represents structured data using six types — string, number, boolean, null, object, and array — and is the default format for REST APIs and most modern config files.
What are the JSON data types?
JSON supports exactly six types: String (double-quoted text), Number (integer or decimal, no quotes), Boolean (true or false, lowercase), Null (null, lowercase), Object (key-value pairs in curly braces), and Array (ordered values in square brackets). JavaScript-specific types like undefined, NaN, and functions are not valid JSON.
Why is my JSON invalid? Common errors
The most common causes are trailing commas after the last item in an object or array, single quotes instead of double quotes, unquoted keys, comments (which JSON does not support), and values like undefined or NaN. The RFC 8259 spec explicitly forbids all of these. Paste your JSON into a formatter or validator to see the exact line and character where the error occurs.
What is the difference between JSON and JavaScript objects?
JSON is a text format — it is always a string. A JavaScript object is a live in-memory data structure. JSON is stricter: all keys must be double-quoted strings, no trailing commas, no comments, and no JavaScript-only types. Convert between them with JSON.parse() (string to object) and JSON.stringify() (object to string).
How do I pretty print JSON?
In JavaScript: JSON.stringify(data, null, 2) where 2 is the indent level. In Python on the command line: python3 -m json.tool file.json. Online: paste into our JSON Formatterand click Format. Pretty-printing only changes whitespace — it does not alter the data.
Can JSON have comments?
No. RFC 8259 explicitly excludes comments from the JSON specification. Any // or /* */ comment will cause a parse error. If you need comments in a config file, use JSONC (supported by VS Code and TypeScript's tsconfig.json), JSON5, or YAML. For API payloads, strip comments before serializing.