What is JSON?
JSON syntax in 5 minutes: objects, arrays, strings, numbers, why it replaced XML, and the gotchas that bite beginners.
JSON is a text format for representing structured data. The acronym stands for JavaScript Object Notation, but it is not just for JavaScript; every modern programming language has a built-in JSON parser. Almost every web API today exchanges JSON. If you have ever logged in, fetched a list of users, or sent a form to a web app, you have used JSON.
What it looks like
A JSON document is one of: an object, an array, a string, a number, a boolean (true /false), ornull. Most real-world JSON is an object at the top level, holding strings, numbers, arrays, and nested objects:
{
"id": 4521,
"name": "Avani Iyer",
"email": "avani@example.com",
"active": true,
"tags": ["fake", "demo"],
"address": null
}
Six rules cover almost everything:
- Keys are strong always | double-quoted strings: code= '"name"' | , never code= "'name'" | or bare code name | .
- String values are double-quoted too. Inside a string, special characters are escaped with backslash: code= '\"' | , code= '\\\\' | , code= '\\n' | .
- Numbers can be integers or floats, with or without a sign and exponent: code 42 | , code -3.14 | , code 1.5e10 | .
- Booleans are code true | or code false | : lowercase, no quotes.
- Null is code null | : lowercase, no quotes.
- No comments. No trailing commas. Both will fail to parse.
Why it replaced XML
Through the early 2000s, web APIs spoke XML (often via SOAP). XML supports schemas, namespaces, attributes vs elements, and a deep ecosystem of tooling. It is also extremely verbose and hard to read by hand. The same data in JSON and XML:
{ "name": "Avani", "active": true }<user><name>Avani</name><active>true</active></user>JSON is shorter, has no separate "is this an attribute or an element" decision, and parses directly into the native data structures of every popular programming language: dicts, hashes, objects, maps. By the early 2010s every new web API was JSON-first; XML stuck around in enterprise SOAP and a few legacy formats.
The gotchas
Strict syntax
JSON parsers reject anything that does not match the grammar. Things you might write out of habit but that are not JSON:
{
'name': 'Avani', // single quotes: invalid
"tags": ["a", "b",], // trailing comma: invalid
// comment: invalid
}
If you need a "JSON with comments" format for config files, look at JSON5, JSONC, or YAML. Strict JSON is for wire protocols.
Numbers do not have a fixed type
JSON specifies numbers as decimal sequences. It does not say whether they are 32-bit or 64-bit, integer or float. Most parsers coerce based on the language: JavaScript turns everything into IEEE-754 doubles (so integers above 2^53 lose precision), Python distinguishesint fromfloat, Go has both. If you are passing huge integer ids (Twitter snowflake ids, Discord ids), serialise them asstrings to avoid silent rounding. The Go REST API keeps ids small enough not to hit this, but it is the most common JSON footgun in production systems.
Order is not guaranteed in objects
The JSON spec says object key order is not significant. Most parsers preserve insertion order, but you should not rely on it. If order matters, use an array of pairs:[["a", 1], ["b", 2]].
Dates are strings
JSON has no date type. The convention is ISO 8601 strings:"2025-09-01T12:34:56Z". Pick a format and stick to it. The Go REST API uses ISO 8601 for every date field.
Working with JSON in code
Every modern language has stdlib JSON. The verbs are usually the same: parse a string into a data structure, and stringify a data structure back to a string.
- JavaScript:
JSON.parse(s)/JSON.stringify(obj) - Python:
json.loads(s)/json.dumps(obj) - Ruby:
JSON.parse(s)/JSON.dump(obj) - Go:
json.Unmarshal([]byte(s), &v)/json.Marshal(v) - Java: Jackson's
ObjectMapperis the standard.
JSON Schema
If you need to validate JSON against an expected shape (for example, in API request bodies), JSON Schema is the most-used standard. You write a schema document (itself JSON) that describes types, required fields, ranges, and patterns. Libraries likeajv (JS),jsonschema (Python),json_schemer (Ruby) can validate input against the schema and return field-level error messages. The Go REST API does not use JSON Schema externally, but most large APIs do.
Try it
Open the JSON beautify/compress tool on this site, paste a sample, and watch it format. Or call any Go REST endpoint and read the JSON it returns. That is what real-world JSON looks like.
More primers
What is a REST API?
A plain-English explanation of REST, HTTP methods, resources, and why almost every modern web service speaks this style.
What is HTTP?
Request/response, headers, methods, status codes - the protocol the web has run on since 1991.
What is a webhook?
Webhooks vs polling: the inverse of an API call, when to use them, and how the receiving endpoint should look.