JSON stands for JavaScript Object Notation. It's a plain-text format for structured data: objects with named fields, lists, text, numbers and true/false values. Almost every web API sends and receives JSON, and many tools use it for configuration. It started in JavaScript but every mainstream language can read and write it.
The format is defined by two matching standards, RFC 8259 and ECMA-404. Files use the .json extension and the media type application/json.
A JSON example
{
"id": 101,
"name": "Ada Lovelace",
"active": true,
"balance": 250.75,
"manager": null,
"address": { "city": "London", "postcode": "N1 9GU" },
"tags": ["admin", "beta"]
} The whole document is an object (curly braces). Each field is a name in double quotes, a colon and a value. address is a nested object and tags is an array (square brackets).
The six JSON data types
| Type | Example | Notes |
|---|---|---|
| String | "London" | Always double quotes. Escape quotes and backslashes with \. |
| Number | 101, 250.75, -3e8 | No leading zeros, no NaN or Infinity. |
| Boolean | true, false | Lower case only. |
| Null | null | "No value". There's no undefined. |
| Object | { "city": "London" } | Unordered name/value pairs; names are strings. |
| Array | ["admin", "beta"] | Ordered list; items can be any type. |
Syntax rules that cause most errors
- Double quotes only.
{'name': 'Ada'}is not JSON; it must be{"name": "Ada"}. - No trailing commas.
[1, 2, 3,]is invalid. - No comments.
//and/* */aren't allowed. Formats such as JSON5 and JSONC add them, but a strict parser rejects them. - Names must be quoted.
{name: "Ada"}is a JavaScript object literal, not JSON. - Special characters in strings must be escaped:
\",\\,\n,\t, or\uXXXX. - Use UTF-8. RFC 8259 requires UTF-8 for JSON exchanged between systems.
Paste any JSON into the JSON validator to see each error with its line and column.
Big numbers lose precision in JavaScript
JavaScript stores numbers as 64-bit floating point, so integers above 9,007,199,254,740,991 (253 − 1) can silently change when parsed: JSON.parse('{"id": 9007199254740993}') gives 9007199254740992. APIs that use 64-bit IDs often send them as strings for this reason.
Reading JSON in code
JavaScript
const user = JSON.parse(text);
console.log(user.address.city);
const out = JSON.stringify(user, null, 2); Python
import json
user = json.loads(text)
print(user["address"]["city"])
out = json.dumps(user, indent=2) Java (Jackson)
ObjectMapper mapper = new ObjectMapper();
JsonNode user = mapper.readTree(text);
System.out.println(user.get("address").get("city").asText()); To work with typed objects instead, generate classes from a sample with JSON to Java, JSON to TypeScript or JSON to Python.
JSON vs XML in one table
| JSON | XML | |
|---|---|---|
| Typical use | Web APIs, configuration | Documents, SOAP, enterprise integration |
| Data types | String, number, boolean, null, object, array | Everything is text unless a schema says otherwise |
| Attributes, namespaces, comments | No | Yes |
| Size | Smaller | Larger (closing tags) |
Need to move between them? Use XML to JSON or JSON to XML, and read XML to JSON conversion: the decisions that matter.
Try it: JSON Validator · JSON Formatter · JSON Viewer. Free, no sign-up.