JSON is a lightweight data interchange format that is easy for humans to read and write. Composed of key-value pairs, it is the standard data format for REST APIs, and although it originated from JavaScript, it is language-independent.
JSON (JavaScript Object Notation) is a text-based, lightweight data interchange format first specified by Douglas Crockford in 2001. It was inspired by JavaScript's object literal syntax, but it is language-independent, so it is natively supported by almost all programming languages, including Python, Java, C#, and Go.
JSON consists of two structures. The first is a collection of name/value pairs (an object), and the second is an ordered list of values (an array). The supported data types are limited to strings, numbers, booleans, null, objects, and arrays, which makes it both simple and powerful. A notable characteristic is that it does not support comments.
The reasons JSON came to replace XML are clear. Compared to XML, it is faster to parse, its data size is smaller, and it is easier to read. When representing the same data, JSON needs no more than half the size of XML. As a result, it has become established as the standard format in REST APIs, configuration files (package.json, tsconfig.json), and NoSQL databases (MongoDB, CouchDB).
In practice, JSON is the standard format for HTTP API communication. It is widely used for data exchange between frontend and backend, microservice communication between servers, storing configuration files, and structuring log data. Extended formats such as JSON5 and JSONC allow comments and trailing commas, so they are used for configuration files, and JSON Schema is used to validate JSON data.
๐ฌ Situation: A team discussion about the API response format
Backend"We're designing a new API response, so please give your opinion on the JSON structure. We want to keep error responses in a consistent format as well."
Frontend"It would be nice if both success and error responses used a wrapper shape like { success: boolean, data: T, error?: { code: string, message: string } }. It makes type guard handling easier on the frontend."
Backend"Sounds good. And how about standardizing dates on the ISO 8601 format (2024-01-15T10:00:00Z) and sending numbers like monetary amounts as strings because of precision issues?"
Frontend"I agree. In JavaScript, large integers lose precision once they exceed Number.MAX_SAFE_INTEGER. And if we document the response structure with JSON Schema, it seems the frontend could also do runtime validation with Zod or io-ts."
โ ๏ธ Cautions
Circular reference error: JSON.stringify() cannot handle objects with circular references. A TypeError is thrown, so if there are circular references, use the flatted library or implement your own handling logic.
Loss of precision in large integers: JSON numbers are parsed as JavaScript Numbers (IEEE 754), so integers of 2^53 or greater lose precision. Send large IDs or monetary amounts as strings, and if you need BigInt, implement separate parsing logic.
Handling special values: undefined, Function, and Symbol are removed by JSON.stringify(), and Infinity and NaN are converted to null. Date is converted to an ISO string, but it does not automatically become a Date object when parsed, so you must use a reviver function.