The Complete Guide to JSON
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that has become the lingua franca of modern software development. Nearly every API you interact with today — from Twitter to Stripe to your company's internal microservices — sends and receives data as JSON.
The format was popularized by Douglas Crockford in the early 2000s, though it was derived from the object literal syntax already present in JavaScript. Crockford recognized that a minimal, language-independent data format would solve the interoperability problems that XML was struggling with. He formalized the specification, which eventually became RFC 8259 (published by the IETF in 2017), the authoritative standard for JSON.
What makes JSON so successful is its simplicity. The entire specification fits on a single business card. It supports exactly six data types, uses a syntax that is both human-readable and trivially machine-parseable, and maps naturally to data structures in virtually every programming language. Python dicts, JavaScript objects, Java HashMaps, Go structs, Ruby hashes — they all have a direct analogue in JSON.
JSON Syntax Rules
A valid JSON document must be either an object (wrapped in curly braces) or an array (wrapped in square brackets) at the top level. Inside these containers, the rules are strict but straightforward:
- Objects are unordered collections of key-value pairs. Keys must be strings enclosed in double quotes. Values can be any valid JSON type. Pairs are separated by commas.
- Arrays are ordered lists of values. Values can be any valid JSON type and are separated by commas.
- Strings must use double quotes. Single quotes, backticks, and unquoted strings are not valid JSON.
- Numbers can be integers or floating-point. Leading zeros are not allowed (except for
0and0.5).NaN,Infinity, and-Infinityare not valid JSON. - Booleans are lowercase
trueorfalse. - Null is lowercase
null. NotNone, notundefined, notnil.
Here is a valid JSON document that demonstrates every data type:
{
"name": "Ada Lovelace",
"age": 36,
"isEngineer": true,
"spouse": null,
"languages": ["English", "French", "Italian"],
"address": {
"city": "London",
"country": "England"
}
}
Common Syntax Errors and How to Fix Them
If you have been working with JSON for any length of time, you have encountered the dreaded "Unexpected token" error. Here are the most common causes and how to fix each one.
Trailing commas — this is the single most common JSON error. JavaScript allows trailing commas in objects and arrays, but JSON does not. If your formatter flags an error on the line after the last property, check for a stray comma.
// Invalid: trailing comma
{"name": "Alice", "age": 30,}
// Valid: no trailing comma
{"name": "Alice", "age": 30}
Single quotes — JSON requires double quotes for all strings and keys. This trips up Python developers (where single and double quotes are interchangeable) and JavaScript developers accustomed to using single quotes by convention.
// Invalid: single quotes
{'name': 'Alice'}
// Valid: double quotes
{"name": "Alice"}
Unquoted keys — in JavaScript, {name: "Alice"} is perfectly valid. In JSON, it is a syntax error. Every key must be a double-quoted string.
Comments — JSON does not support comments. Not //, not /* */, not #. This was a deliberate design decision by Douglas Crockford to prevent comments from being used as parsing directives (a problem that plagued XML). If you need comments in config files, consider JSON5 or JSONC (the format VS Code uses for settings.json).
Special JavaScript values — undefined, NaN, Infinity, and functions are valid JavaScript values but are not part of the JSON specification. If you use JSON.stringify() on an object containing undefined, those keys are silently dropped. NaN and Infinity will throw an error.
How to Format JSON
Minified JSON — a single line of densely packed text with no whitespace — is great for transmission but terrible for reading. Formatting (also called "pretty-printing" or "beautifying") adds line breaks and indentation so you can actually see the structure.
Consider this minified response from an API:
{"users":[{"id":1,"name":"Alice","roles":["admin","editor"]},{"id":2,"name":"Bob","roles":["viewer"]}]}
After formatting with 2-space indentation, the same data becomes immediately legible:
{
"users": [
{
"id": 1,
"name": "Alice",
"roles": [
"admin",
"editor"
]
},
{
"id": 2,
"name": "Bob",
"roles": [
"viewer"
]
}
]
}
The data is identical. But the formatted version lets you instantly see that there are two users, Alice has two roles, and Bob has one. When you are debugging a 500-line API response at 2 AM, this difference matters enormously.
The tool above lets you switch between 2-space, 4-space, and tab indentation depending on your team's convention. There is no "right" answer — 2 spaces is the most common in JavaScript/TypeScript projects, 4 spaces is popular in Python and Java ecosystems, and tabs are the accessibility-friendly option (users can configure their display width).
JSON in API Development
JSON dominates API development. The vast majority of REST APIs use JSON as their request and response format, and GraphQL exclusively returns JSON. Understanding how to read, format, and validate JSON is a foundational skill for anyone working with APIs.
When debugging API responses, your first step should always be to paste the raw response into a formatter. Minified JSON from curl or browser DevTools is nearly impossible to read without formatting. A quick curl -s https://api.example.com/users | python -m json.tool on the command line, or pasting into a tool like this one, saves minutes of squinting at dense text.
For programmatic use, the fetch() API in JavaScript makes JSON handling seamless: response.json() parses the response body into a native JavaScript object. In Python, the requests library offers response.json() for the same purpose. And command-line tools like jq let you slice, filter, and transform JSON directly in your terminal — making it indispensable for shell scripting and pipeline debugging.
Why JSON won over XML: JSON's rise came at the expense of XML, which had dominated data interchange throughout the late 1990s and early 2000s. JSON won because it is dramatically simpler (no closing tags, no attributes vs. elements distinction, no DTDs or namespaces), maps more naturally to programming data structures, and produces significantly smaller payloads. A JSON representation of the same data is typically 30-50% smaller than its XML equivalent.
JSON Schema Validation
Syntax validation (which this tool provides) tells you whether a JSON document is well-formed. But in production systems, you often need schema validation — verifying that the JSON conforms to an expected structure. Does the email field exist? Is age a positive integer? Are all required fields present?
JSON Schema is a vocabulary that lets you define the structure, constraints, and documentation of your JSON data. It is itself written in JSON, and it has become the industry standard for API contract validation, form generation, and documentation. Major tools like Swagger/OpenAPI, Postman, and Ajv all use JSON Schema under the hood.
A basic JSON Schema for a user object might look like this:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
}
}
This schema guarantees that any conforming document has a non-empty name string and a valid email, with an optional non-negative age. Schema validation catches entire classes of bugs that syntax validation alone cannot — missing fields, wrong types, out-of-range values — before they reach your application logic.
Tips for Working with JSON
- Always format before reading. Do not try to parse minified JSON with your eyes. Paste it into a formatter first. Your future self will thank you.
- Validate before sending. When constructing JSON payloads manually (in tests, scripts, or config files), validate the syntax before sending it to an API. A missing comma or extra quote can cost you 30 minutes of debugging a "server error" that is actually a malformed request.
- Minify for production. If you are embedding JSON in web pages or sending it over the wire, minify it. Removing whitespace can reduce payload size by 20-40%, which adds up at scale.
- Learn
jqfor command-line work. Thejqtool is a lightweight command-line JSON processor that lets you filter, map, and transform JSON with a concise query syntax.cat data.json | jq '.users[] | .name'extracts all user names from an array. It is one of those tools that pays for itself within a day of learning. - Use
JSON.stringify(obj, null, 2)in JavaScript. The third argument controls indentation. This is the fastest way to pretty-print JSON in a Node.js script or browser console. - Watch for encoding issues. JSON must be UTF-8 encoded (per RFC 8259). If you see garbled characters (
\u00e9instead of accented letters), the JSON is likely using Unicode escape sequences — which are valid but harder to read. Most formatters will display the actual characters.