API Directory โ€บ Guides โ€บ What Is JSON? The Language APIs Speak

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:

StringText in double quotes: "Lagos". Single quotes are not allowed.
Number15388000, -4.5, 1e3. No distinction between int and float; no NaN or Infinity.
Booleantrue or false (lowercase).
nullThe absence of a value.
ArrayAn ordered list in square brackets: [1, 2, 3]. Items can be any type, even mixed.
ObjectAn unordered set of "key": value pairs in curly braces. Keys must be quoted strings.

Rules that trip people up

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

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’