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:
399or4.5 - boolean:
trueorfalse - 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.