In this guide
What is JSON?
JSON stands for JavaScript Object Notation. It's a lightweight, text-based format for representing structured data. Despite the JavaScript in the name, JSON is language-independent — it works with Python, Go, Java, Ruby, PHP, and every other modern programming language.
JSON was created by Douglas Crockford in the early 2000s as a simpler alternative to XML. Today it's the de facto standard for API responses — virtually every modern REST API sends and receives data as JSON. When you see the text application/json in a request or response, that's JSON in action.
JSON syntax
JSON is built on two structures: objects (key-value pairs) and arrays (ordered lists). Here's what a real API response looks like:
"id": 42,
"name": "Alice Nguyen",
"email": "[email protected]",
"verified": true,
"score": 98.5,
"tags": ["admin", "beta"],
"address": {
"city": "San Francisco",
"country": "US"
},
"middleName": null
}
Four rules cover almost all of JSON's syntax:
- Objects are wrapped in curly braces
{ } - Arrays are wrapped in square brackets
[ ] - Keys are always strings in double quotes
- Key-value pairs are separated by commas (no trailing comma after the last item)
The six JSON data types
Every value in JSON must be one of exactly six types. Knowing them helps you parse responses confidently.
"hello", "2026-06-14", "USD". Dates and IDs are often strings even if they look like numbers.42, 3.14, -100. JSON doesn't distinguish int from float — your language does.true or false (lowercase, no quotes). Used for flags: "active": true, "verified": false.null. Different from an empty string "" or zero 0 — null means the value doesn't exist.{ }. Objects can be nested — an address object inside a user object, for example.[ ]. Each item can be any JSON type — you can have an array of objects, an array of strings, even an array of arrays.How APIs use JSON
APIs use JSON in both directions: you send JSON in the request body when creating or updating data, and you receive JSON in the response body when reading data.
Sending JSON (POST request)
When you create a new resource — like submitting an order — you send JSON in the request body. You also set the Content-Type: application/json header so the server knows what format to expect.
Content-Type: application/json
{
"productId": "SKU-9001",
"quantity": 2,
"shippingAddress": {
"street": "123 Main St",
"city": "Austin"
}
}
Receiving JSON (GET response)
When you fetch data, the server responds with JSON. APIs often wrap the data inside an envelope with metadata about the response.
{
"data": [
{ "id": 1, "name": "Widget A", "price": 9.99 },
{ "id": 2, "name": "Widget B", "price": 14.99 }
],
"total": 2,
"page": 1
}
Reading a JSON response
Real API responses are often deeply nested. The key skill is knowing how to navigate from the top level to the value you want.
Take this weather API response. To get the temperature you need to follow the path: current → temp_c.
"location": {
"name": "London",
"country": "UK"
},
"current": {
"temp_c": 18.5, ← this is what we want
"condition": {
"text": "Partly cloudy"
}
}
}
data.current.temp_c, check that the parent objects exist. If current is null or missing and you try to access .temp_c, your code will throw an error. Defensive access (data?.current?.temp_c in JavaScript) or explicit null checks are good habits when working with API responses.
JSON in different languages
Every major language has built-in support for parsing JSON strings into native data structures and serialising them back.
JavaScript
const data = JSON.parse('{"name":"Alice","age":30}');
console.log(data.name); // "Alice"
// Convert an object to a JSON string
const json = JSON.stringify({ name: "Alice", age: 30 });
console.log(json); // '{"name":"Alice","age":30}'
Python
# Parse a JSON string into a Python dict
data = json.loads('{"name": "Alice", "age": 30}')
print(data["name"]) # Alice
# Convert a dict to a JSON string
json_str = json.dumps({"name": "Alice", "age": 30})
print(json_str) # {"name": "Alice", "age": 30}
Working with the fetch API (JavaScript)
When you call a REST API with fetch, the response body is a raw stream. Call .json() to parse it automatically:
const data = await response.json(); // parses JSON automatically
console.log(data.name); // access fields directly