JSON Explained for Beginners
JSON (JavaScript Object Notation) is a plain-text format for structured data. Web APIs use it to send and receive information, apps use it for configuration and save files, and essentially every programming language can read and write it. If you work with code, data, or config at all, you will meet JSON within the week.
Why it took over
Before JSON, structured data on the web mostly travelled as XML, which is verbose and needs a heavyweight parser. JSON is compact, maps directly onto the data structures programmers already use (objects/dictionaries and arrays/lists), and is trivial to parse. It was standardised as ECMA-404 and RFC 8259, and the spec is deliberately tiny — the whole grammar fits on a page.
The six value types
A JSON value is exactly one of these:
- String — text in double quotes:
"hello". Special characters are backslash-escaped:"\n"newline,"\t"tab,"\""a literal quote,"\\"a backslash,"é"a Unicode code point. - Number —
42,-3.14,2.5e6. No quotes, no leading zeros (01is invalid), no+sign on the front, and crucially noNaNand noInfinity. - Boolean —
trueorfalse, always lowercase. - Null —
null, lowercase. Represents “no value.” - Array — an ordered list in square brackets:
[1, "two", true, null]. Elements can be any mix of types. - Object — an unordered set of key/value pairs in curly braces:
{"name": "Ada", "age": 36}. Keys must be double-quoted strings.
Arrays and objects nest to any depth, which is how JSON represents complex, tree-shaped data.
A complete annotated example
{
"site": "QuickToolz",
"launched": 2026,
"free": true,
"maintainer": null,
"tools": ["qr-code-generator", "json-formatter", "password-generator"],
"contact": {
"email": "hello@example.com",
"hoursUTC": [9, 17]
}
}
That single object contains a string, a number, a boolean, a null, an array of strings, and a nested object which itself holds a string and an array of numbers. Any JSON parser turns this into the native map/list types of its language.
The rules that trip people up
- Double quotes only.
'single quotes'are not valid JSON, for strings or keys. - No trailing commas.
[1, 2, 3,]and{"a": 1,}are both invalid. This is the single most common error, because JavaScript object literals do allow them. - Keys must be quoted.
{name: "Ada"}is invalid;{"name": "Ada"}is correct. - No comments.
// like thisand/* this */are not part of JSON. Formats that allow comments (JSON5, JSONC) are different formats that happen to look similar. - No functions, dates, or undefined. JSON has no date type — dates travel as
strings (usually ISO 8601,
"2026-09-02T12:00:00Z") and are parsed by the application. - UTF-8, no byte-order mark. Save files as UTF-8 without a BOM; a leading BOM makes many strict parsers choke on the first character.
- The top level can be any value.
42,"hello",true, and[1,2,3]are all valid JSON documents on their own, not just objects.
How to read a parser error
When a parser rejects JSON it reports the first place it got stuck, typically as
Unexpected token X in JSON at position N, Unexpected string, or Unexpected end of JSON input. A method that works:
- Look just before the reported position. The real mistake is often one character earlier — a missing comma between two items, or a missing closing quote that made the parser read too far.
- Check that brackets balance. Every
{needs a}, every[needs a], correctly nested. An editor that highlights matching brackets finds these fast. - “Unexpected end of input” almost always means a missing closing
}or]at the end. - For a large document, paste a small chunk you believe is correct, confirm it validates, then grow it until it breaks — that isolates the bad section.
The JSON formatter validates as it pretty-prints and shows the exact error message and location, so you can fix problems before they reach your code or your API call.
Working with JSON in practice
Every mainstream language has a built-in parser and serialiser:
| Language | Parse | Produce |
|---|---|---|
| JavaScript | JSON.parse(str) |
JSON.stringify(obj, null, 2) |
| Python | json.loads(str) |
json.dumps(obj, indent=2) |
| Ruby | JSON.parse(str) |
JSON.generate(obj) / obj.to_json |
| Go | json.Unmarshal(b, &v) |
json.MarshalIndent(v, "", " ") |
| Java | Jackson / Gson readValue |
writeValueAsString |
| PHP | json_decode($str, true) |
json_encode($v, JSON_PRETTY_PRINT) |
The null, 2 / indent=2 arguments pretty-print with two-space indentation —
the same thing the JSON formatter does in the browser. On the
command line, jq is the standard tool for filtering and reshaping JSON, and
python -m json.tool pretty-prints a file.
Numbers, dates, and precision
Two traps that bite real projects:
- Big integers. JSON numbers are decimal and unbounded on paper, but many parsers read them into a 64-bit float (JavaScript does), which loses precision above 2⁵³ (about 9 quadrillion). IDs like Twitter/X snowflake IDs or database bigints are usually sent as strings for this reason. If you control both ends and need exact large integers, quote them.
- Dates. JSON has no date type. The near-universal convention is an
ISO 8601 string in UTC:
"2026-09-02T14:30:00Z". The application parses it into its own date type. Sending a raw millisecond timestamp (1788446400000) also works but is less readable and hits the big-integer issue. - Floats.
0.1 + 0.2is not exactly0.3in binary floating point, in JSON as in most languages. For money, send integer cents (1299) or a quoted decimal string ("12.99"), never a bare float.
Security notes
- Never build JSON by string concatenation. Use your language’s serialiser, which escapes quotes and control characters correctly. Hand-built JSON is how injection bugs and malformed payloads happen.
- Do not
eval()JSON in JavaScript — useJSON.parse.evalwill run code hidden in the string. - Validate the shape of JSON you receive from users or other services (a schema library, or manual checks) before trusting field types and ranges.
- Be aware of duplicate keys (
{"a":1,"a":2}) — the spec allows them and most parsers keep the last, which can be abused to smuggle values past a validator that reads the first.
JSON vs its lookalikes
- JSON5 / JSONC — allow comments, trailing commas, unquoted keys, single quotes. Nice for hand-edited config; not interchangeable with strict JSON.
- NDJSON / JSON Lines — one JSON value per line, no enclosing array. Used for streaming and log files.
- YAML — a superset-ish config format; every JSON document is also valid YAML, but not vice versa.
The bottom line
JSON is six value types — string, number, boolean, null, array, object — with a strict, tiny grammar: double quotes everywhere, no trailing commas, no comments, quoted keys. When a parser complains, look one character before the reported position and check your brackets. Validate and format with the JSON formatter before shipping it.