JSON in REST APIs
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
JSON stands for JavaScript Object Notation. It is a text format for structured data, not a JavaScript-only object. A JSON value can be an object, array, string, number, true, false, or null. Objects contain named members, while arrays contain an ordered list of values. Almost every common language has a parser for this small grammar.
JSON became the common web API default because its syntax is compact, browser clients handle it naturally, and mature libraries map it to ordinary values in Java, JavaScript, Python, Go, and other languages. A Java service can return an order DTO and a browser can read the same representation without an XML document model or a platform-specific binary decoder.
HTTP identifies the body format separately from the resource. Content-Type: application/json says that the content being sent is JSON. A client can send Accept: application/json to ask for a JSON response when the resource has more than one representation. JSON exchanged between systems uses UTF-8, and the API still has to document the fields, allowed values, and response status.
The parser can accept {"totalMinor":1999,"currency":"INR"}, but JSON alone does not say that 1999 means ₹19.99, that the currency is supported, or that the amount must be positive. Those rules belong to the API schema and domain. A request can therefore be valid JSON and still be rejected as invalid input.
JSON has no built-in date, exact-decimal, binary, comment, or reference type, so an API chooses conventions such as an ISO 8601 string for an instant and integer minor units for money. Large numeric identifiers are often strings to avoid cross-language precision loss. REST and HTTP do not require JSON; the same resource can also have HTML, XML, a file, or another negotiated representation.
JSON in REST APIs
Direct Answer
JSON is a text format for structured values: objects, arrays, strings, numbers, booleans, and null. It became the common default for web APIs because it is compact, language-independent, natural for browser clients, and supported by mature tools. It is a representation format, not a requirement of REST or HTTP.
Deep Dive & Architecture
A JSON parser can tell whether braces, strings, commas, and values follow the grammar. It cannot tell whether amount may be negative, whether createdAt is a timestamp, or whether status accepts paid. Those rules belong to the API schema and domain contract. Valid JSON can still be an invalid request.
Object member names should be unique for interoperable behavior. Different parsers have historically handled duplicate names differently, often keeping only the last value. An API should reject or prevent duplicates rather than depend on one parser's choice. Member order should not carry business meaning; array order can.
Numbers deserve special attention across languages. JSON's grammar does not set one machine representation, but JavaScript numbers cannot exactly represent every large integer. IDs are often strings when their range or formatting matters. Money should use a documented exact representation instead of a binary floating-point assumption.
The media type tells a recipient how to interpret the bytes. application/json plus UTF-8 is the interoperable default for open exchange. Content negotiation allows another representation where it genuinely helps, such as a file download or a compact binary protocol. JSON's popularity is a practical ecosystem advantage, not an architectural mandate.
Code Example
{
"id": "O-42",
"createdAt": "2026-09-07T10:15:30Z",
"totalMinor": 1999,
"currency": "INR",
"items": [
{"productId": "P-9", "quantity": 2}
]
}
JSON validates the structure only. The API contract explains that createdAt is an instant, totalMinor uses the currency's minor unit, and quantity must be positive.
Check your understanding
Answer these without looking back at the article.
- Which value types exist in JSON?
- Why might an API represent an identifier as a JSON string?
- Does a REST API have to use JSON?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.