JSON — short for JavaScript Object Notation — is the lingua franca of modern web APIs, configuration files, and data exchange. If you've ever called a REST API, edited a package.json file, or stored settings in a SaaS app, you've worked with JSON. This guide walks through what JSON is, how it works, and how to avoid its most common pitfalls.
What Is JSON?
JSON is a lightweight, text-based, language-independent data format. It was originally derived from JavaScript object literals but is now supported by virtually every programming language. The official spec is RFC 8259.
Two things make JSON popular: it's human-readable, and it maps cleanly to the data structures most languages already have (objects, arrays, strings, numbers).
JSON Syntax in 5 Minutes
A JSON document is built from just six types of values:
- Object — a collection of key/value pairs in curly braces:
{"name": "Alice"} - Array — an ordered list in square brackets:
[1, 2, 3] - String — text in double quotes:
"hello" - Number — integer or float:
42or3.14 - Boolean —
trueorfalse - Null — the absence of a value:
null
Here's a realistic example:
{
"name": "QuickTools",
"version": "1.0",
"free": true,
"tools": ["Word Counter", "Password Generator"]
}
The Rules You Must Remember
JSON is strict. These rules trip up beginners daily:
- Strings must use double quotes. Single quotes are invalid.
- Object keys are always strings in double quotes — even numeric-looking keys.
- No trailing commas after the last item in an object or array.
- No comments. JSON has no
//or/* */syntax. - Numbers don't have a leading zero (except
0itself) or trailing decimal point.
Common JSON Errors and How to Fix Them
Unexpected token
Usually caused by a missing comma, a stray comma at the end, or single quotes instead of double quotes. Run your JSON through our JSON Formatter and the error message will tell you the exact position.
Unexpected end of input
You're missing a closing } or ]. Beautify the JSON to see the structure clearly.
Bad escape sequence
Special characters inside strings need escaping: use \" for quotes, \\ for backslashes, \n for newlines.
JSON vs Other Formats
JSON isn't perfect for every situation. Quick comparison:
- YAML — supports comments and is more human-readable, but indentation-sensitive (easy to break).
- XML — more verbose, supports attributes and schemas; useful in enterprise systems.
- TOML — popular for config files (used by Rust's Cargo); easier for humans, less flexible.
- Protocol Buffers — binary format, much smaller and faster but not human-readable.
Beautify vs Minify
JSON has two common forms:
- Beautified — indented and spread over many lines. Best for reading and debugging.
- Minified — no whitespace, all on one line. Best for storage and network transmission because it's smaller.
Our JSON Formatter tool handles both with one click.
Working with JSON Programmatically
In JavaScript:
const obj = JSON.parse(jsonString); // parse to object
const str = JSON.stringify(obj, null, 2); // back to formatted string
Most languages have similar built-in support — Python has json, Go has encoding/json, Ruby has JSON.parse, and so on.
Security Considerations
Never eval() a JSON string. Always use the safe parser provided by your language. If you accept JSON from users, validate the structure and sanitize values before using them in queries or rendering them in HTML.
Conclusion
JSON's simplicity is what makes it so successful. With six value types and a handful of strict rules, you can describe almost any data structure in a way both humans and machines can read. Bookmark our JSON Formatter and you'll never wrestle with a syntax error again.