What Is JSON? The Language APIs Speak
Almost every API returns JSON. Learn what it is, how to read it, and how to work with it โ in five minutes.
JSON (JavaScript Object Notation) is a lightweight text format for representing structured data. It is the default language APIs use to send and receive information, because it is easy for people to read and trivial for every programming language to parse.
The whole format in one example
{
"city": "Lagos",
"population": 15388000,
"coastal": true,
"timezone": null,
"districts": ["Ikeja", "Surulere", "Yaba"],
"mayor": { "name": "โ", "elected": 2023 }
}That is essentially all of JSON. It has just six value types:
| String | Text in double quotes: "Lagos". Single quotes are not allowed. |
| Number | 15388000, -4.5, 1e3. No distinction between int and float; no NaN or Infinity. |
| Boolean | true or false (lowercase). |
| null | The absence of a value. |
| Array | An ordered list in square brackets: [1, 2, 3]. Items can be any type, even mixed. |
| Object | An unordered set of "key": value pairs in curly braces. Keys must be quoted strings. |
Rules that trip people up
- Every key and every string value must use double quotes.
- No trailing commas —
[1, 2, 3,]is invalid. - No comments — JSON has no
//or/* */. - Dates are not a JSON type — they are sent as strings, almost always ISO 8601 (
"2026-01-15T09:30:00Z"). - Large integers can lose precision in languages that parse numbers as 64-bit floats (JavaScript). Some APIs send IDs as strings for this reason.
Working with JSON in code
# Python
import json, requests
data = requests.get(url).json() # parse response JSON
print(data["districts"][0]) # "Ikeja"
payload = json.dumps({"q": "Lagos"}) # object -> JSON string
// JavaScript
const data = await (await fetch(url)).json();
console.log(data.districts[0]);
const payload = JSON.stringify({ q: "Lagos" });The two operations are always the same: parse (JSON text → native object) when you receive a response, and serialize (native object → JSON text) when you send a request body. Set Content-Type: application/json when you send JSON, and Accept: application/json to ask for it back.
Reading a nested response
API responses are often deeply nested. Trace the path key by key: response["results"][0]["geometry"]["lat"]. When a field might be missing or null, guard for it (data.get("timezone") in Python, optional chaining data?.timezone in JavaScript) rather than letting the whole parse crash.
FAQ
Is JSON the same as a JavaScript object?
It is based on JavaScript object syntax, but JSON is a pure text format with stricter rules (quoted keys, no functions, no comments, no undefined). Every JSON document is valid text in any language.
How do I check if a JSON string is valid?
Paste it into a JSON validator/formatter, or try to parse it (json.loads / JSON.parse) and catch the error. The error message usually points at the offending character.
Why are numbers in the API response strings?
Often to preserve precision (IDs, monetary amounts, blockchain values) that would be corrupted if parsed as a floating-point number. Keep them as strings and use a decimal library for maths.
What is JSON used for besides APIs?
Configuration files (package.json, tsconfig.json), log lines, message queues, and browser localStorage. It is the default data-interchange format of the modern web.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ