Skip to the workspace
Pebellslearn by building
Designing APIs  ›  Lesson

JSON APIs — Predictable Responses

JSON APIs 12 minA good JSON API returns the same shape every time, uses the status code to signal success or failure, and describes errors in a consistent JSON body.

backend / JSON APIs — Predictable Responses

Explore each scene at your own pace.

01 · The rule

The rule

JSON APIs — Predictable Responses

The mobile team writes code against your API once and expects it to keep working. If /books/42 sometimes returns a list, sometimes an object, and signals errors with a 200 and a random message, every client ends up full of special cases. A predictable JSON shape is a promise that lets other people build on you.

The rule

Same shape every time; the status says whether it worked.

JSON is the text format nearly every web API uses. It has just six kinds of value:

  • object: {"title": "The Salt Road", "price": 399}
  • array: [1, 2, 3]
  • string: "text", always in double quotes
  • number: 399 or 4.5
  • boolean: true or false
  • null: null

Keys must be double-quoted strings. There are no comments, no trailing commas and no date type (dates are usually strings like "2026-09-25").

A predictable API picks shapes and never varies them:

{
  "id": 42,
  "title": "The Salt Road",
  "price": 399,
  "author": {"id": 7, "name": "Meera Iyer"},
  "published_on": "2019-03-14"
}

Lists come wrapped with paging information, so fields can be added later without breaking clients:

{
  "items": [{"id": 42, "title": "The Salt Road"}],
  "page": 1,
  "total": 5000
}

Errors get an honest status code and one consistent body shape:

{
  "error": {
    "code": "book_not_found",
    "message": "No book with id 9999"
  }
}

The code is stable text that programs can check; the message is for humans and may change.

Consistency also covers the small choices. Pick one naming style for keys, such as snake_case, and use it everywhere. Pick one format for dates. Send money as whole numbers in the smallest unit (rupees here, or paise), because decimal fractions invite rounding surprises.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.