EMZETT.
Login

JSON

In short: “JavaScript Object Notation” — a compact, text-based data format for structured data (objects, arrays, numbers, strings, booleans), the de-facto standard for data exchange between web APIs and applications.

In more detail: Despite the name, JSON isn’t tied to JavaScript — practically every programming language can read and write JSON. Compared to XML, it’s considerably more compact (no closing tags) and natively supported by JavaScript/TypeScript (JSON.parse()/JSON.stringify()), which is why it has replaced XML for most new REST APIs.

In Depth

{
  "name": "Emzett Wiki",
  "categories": ["Network", "Hardware", "Software"],
  "active": true,
  "articleCount": 640
}

JSON only has a small, fixed set of data types: objects ({}, key-value pairs), arrays ([]), strings, numbers, booleans and null — deliberately kept simple, which makes it easy to implement a JSON library in practically any language. Unlike XML, JSON has no built-in mechanism for comments or data-type validation against a schema (separate standards like JSON Schema exist for that).

Because JSON is so close to JavaScript’s own object syntax, it’s especially easy to process there — in most other languages, an explicit library is needed to convert a JSON string into native data structures (parsing) and back (serialisation). JSON’s compactness and easy readability have made it the standard format for REST APIs since the 2000s, where it has largely replaced XML.

Limits of the format

JSON has deliberate limitations that regularly show up in practice: there’s no native date type (dates are usually transmitted as an ISO 8601 string and have to be parsed by the receiver), no support for comments (which makes configuration files in pure JSON unwieldy — which is why many tools use JSONC or YAML for configs instead), and numbers aren’t distinguished between integer and floating point, which can lead to precision loss with very large integers (e.g. 64-bit IDs), since JavaScript internally stores numbers as 64-bit floating-point values.

Several JSON extensions have emerged from these limitations: JSON5 allows comments and unquoted keys for better readability in configuration files, NDJSON (Newline Delimited JSON) lines up several JSON objects one per line and is suited for streaming large amounts of data without having to keep the entire document in memory, and JSON Schema defines a vocabulary for validating the expected structure of a JSON document — useful for documenting API contracts in a machine-readable way and checking them automatically.

Practical use in Next.js/TypeScript

In TypeScript projects, JSON is often combined with zod or similar validation libraries, to convert unstructured JSON data at runtime (e.g. from an API response) into type-safe objects — plain JSON.parse() only returns any, since TypeScript can’t guarantee the actual structure without explicit validation.

const response = await fetch("/api/user");
const raw = await response.json(); // type: any
const user = userSchema.parse(raw); // type: validated User object

See also: XML, API, REST