What is JSON?

JSON is the language APIs speak. Here's how to read it and work with it in your code.

← Learn

In this guide

  1. What is JSON?
  2. JSON syntax
  3. The six JSON data types
  4. How APIs use JSON
  5. Reading a JSON response
  6. JSON in different languages

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.

Analogy: JSON is like a labelled filing system. Instead of a messy drawer of data, every piece of information has a clear label and structure. When an API sends you a user's profile, it doesn't just send random text — it sends a neatly labelled package: "name is Alice", "age is 30", "email is [email protected]".

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:

The six JSON data types

Every value in JSON must be one of exactly six types. Knowing them helps you parse responses confidently.

String
Text, always in double quotes. Examples: "hello", "2026-06-14", "USD". Dates and IDs are often strings even if they look like numbers.
Number
Integer or decimal, no quotes needed. Examples: 42, 3.14, -100. JSON doesn't distinguish int from float — your language does.
Boolean
Either true or false (lowercase, no quotes). Used for flags: "active": true, "verified": false.
Null
Represents an absent or unknown value: null. Different from an empty string "" or zero 0 — null means the value doesn't exist.
Object
A collection of key-value pairs inside { }. Objects can be nested — an address object inside a user object, for example.
Array
An ordered list of values inside [ ]. 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.

POST /orders
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.

HTTP 200 OK

{
  "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: currenttemp_c.

{
  "location": {
    "name": "London",
    "country": "UK"
  },
  "current": {
    "temp_c": 18.5,  ← this is what we want
    "condition": {
      "text": "Partly cloudy"
    }
  }
}
Always check for null: Before accessing a nested value like 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

// Parse a JSON string into a JavaScript object
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

import json

# 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 response = await fetch("https://api.example.com/users/42");
const data = await response.json(); // parses JSON automatically
console.log(data.name);             // access fields directly

Continue learning

What is an API?
Start here if you're new to APIs
What is REST?
HTTP methods and endpoints
What is an API key?
Auth types and safe storage