JSON
13 tools
What Is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Originally derived from JavaScript, JSON is language-independent and is now the de facto standard for data exchange on the web.
JSON represents data as key-value pairs organized into objects and arrays. Its simplicity made it the dominant format for REST APIs, configuration files, and data storage — replacing the more verbose XML in most modern applications.
A Brief History
JSON was formalized by Douglas Crockford in the early 2000s, though the format itself was already implicit in JavaScript syntax. The specification was published at json.org in 2001. RFC 4627 followed in 2006, and the current ECMA-404 standard was published in 2013.
Before JSON, XML was the dominant data exchange format on the web. JSON's minimalism — no attributes, processing instructions, or namespaces — made it immediately popular with developers building AJAX applications. By 2010, JSON had largely displaced XML for web APIs.
JSON Data Types
JSON supports exactly six data types. Every value in a valid JSON document must be one of these:
"hello world"Any sequence of Unicode characters enclosed in double quotes. Backslash escape sequences (\n, \t, \") are supported.
42 / 3.14 / 1e10Integer or floating-point. Scientific notation (1e10) is allowed. No distinction between int and float at the JSON level.
true / falseThe literal tokens true or false (lowercase only). There is no truthy/falsy — only strict boolean.
nullThe literal token null (lowercase). Represents absence of value. Different from undefined, which is not a JSON type.
[1, "two", null]An ordered list of values enclosed in square brackets. Elements can be of mixed types and arrays can be nested arbitrarily.
{"key": "value"}An unordered collection of key-value pairs enclosed in curly braces. Keys must be strings. Values can be any JSON type.
{
"string": "hello world",
"number": 42,
"float": 3.14,
"boolean": true,
"null": null,
"array": [1, 2, 3],
"object": { "nested": "value" }
}JSON Syntax Rules
JSON has strict syntax rules. A small deviation causes a parse error. Here are the six most important rules to remember:
All string values and object keys must use double quotes. Single quotes are not valid JSON. This is the most common mistake for developers coming from JavaScript.
JSON does not allow trailing commas after the last element in an array or the last property in an object. [1, 2, 3,] and {"a": 1,} are both invalid.
JSON has no comment syntax. // and /* */ are not valid. If you need comments in config files, consider JSON5 or YAML, which both support comments.
JSON only supports six primitive types. JavaScript values like undefined, NaN, Infinity, and functions cannot be represented and are either omitted or converted to null during serialization.
All object keys must be quoted strings. Numeric keys, bare identifiers, or other types are not allowed. Keys should be unique, though most parsers accept duplicates.
JSON is case-sensitive. true, false, and null must be all lowercase. TRUE, False, NULL, or Null are invalid tokens and will cause a parse error.
Invalid vs. Valid JSON
{
'name': 'Alice', // single quotes — invalid
age: 30, // unquoted key — invalid
"scores": [1, 2, 3,], // trailing comma — invalid
"note": undefined // undefined — invalid
}{
"name": "Alice",
"age": 30,
"scores": [1, 2, 3],
"note": null
}JSON in the Modern Web
REST APIs
JSON is the standard body format for REST API requests and responses. Content-Type: application/json is the most common MIME type on the modern web. Virtually every programming language has a built-in JSON parser, making interoperability seamless.
Configuration Files
package.json, tsconfig.json, .eslintrc, and many other developer tools use JSON for configuration. The structured format and universal parser support make it practical, though the lack of comments is a persistent criticism.
NoSQL Databases
Document databases like MongoDB, CouchDB, and Amazon DynamoDB store data as JSON or JSON-like documents. This allows flexible, schema-less storage that maps naturally to application objects.
Frontend State and Data
localStorage and sessionStorage store strings, so JSON.stringify/JSON.parse are used constantly to serialize JavaScript objects. API responses from fetch() arrive as JSON text and are parsed before use.
JSON vs. Other Formats
JSON is not always the best choice. Here is how it compares to other common data formats:
Simpler syntax, smaller file size, easier to parse, more readable for developers
No support for attributes, processing instructions, or XML namespaces
You need document metadata, mixed content models, or XML schemas (XSD/XSLT)
Stricter syntax (less ambiguity), better tooling support, more portable
No comment support, slightly more verbose for deeply nested data
Human-editable config files, Docker Compose, GitHub Actions, Kubernetes manifests
Wider ecosystem support, faster parsing, more predictable type handling
No native date type, less human-friendly for complex nested structures
Rust (Cargo.toml), Python (pyproject.toml), INI-style application configuration
Structured and nested data, not limited to flat rows and columns
Much larger file size, worse performance for purely tabular data
Spreadsheet data, database exports, data to be opened in Excel or pandas
JSON Schema
JSON Schema is a vocabulary for validating the structure of JSON documents. A schema describes the expected types, required fields, and constraints (minimum, maximum, pattern) of a JSON value. The current version is JSON Schema 2020-12.
Tools like ajv (JavaScript), jsonschema (Python), and built-in validators in many IDEs support JSON Schema. OpenAPI (formerly Swagger) uses JSON Schema to describe API request and response bodies, making it the backbone of modern API documentation.
Common JSON Pitfalls
JSON numbers are parsed as IEEE 754 double-precision floats. Integers larger than 2^53 lose precision. Pass large IDs (Twitter snowflakes, database bigints) as strings, not numbers.
JSON has no date type. Dates are typically serialized as ISO 8601 strings or Unix timestamps. Your code must know which convention was used — there is no way to infer it from the JSON itself.
JSON.stringify throws a TypeError on circular object references. You need to break the cycle or use a library that handles it (json-stringify-safe, flatted).
JSON must be encoded as UTF-8, UTF-16, or UTF-32. UTF-8 is standard. A Byte Order Mark (BOM) at the start of a JSON file is technically non-compliant and breaks some parsers.
When parsing untrusted JSON, merging parsed objects into existing ones can be vulnerable to prototype pollution attacks via the __proto__ key. Always validate or sanitize JSON from external sources.
{"key": null} and a missing "key" are semantically different. Null means the field exists with an absent value; missing means the field was not specified. JSON Schema can express both distinctions.
Frequently Asked Questions
JSON stands for JavaScript Object Notation. Despite the name, JSON is completely language-independent and is supported in virtually every programming language.
No. JSON is a text format that resembles a JavaScript object literal, but it has stricter rules: keys must be double-quoted, and it does not support undefined, functions, or comments. A JSON document is always a string, not an in-memory object.
No. The JSON specification does not include comment syntax. If you add // or /* */ comments, your file is no longer valid JSON. For config files that need comments, consider JSONC, JSON5, or YAML.
JSONP (JSON with Padding) was a workaround for the browser same-origin policy that wrapped JSON in a function call. It is obsolete — use CORS instead. Modern browsers fully support Cross-Origin Resource Sharing.
In JavaScript: JSON.stringify(data, null, 2) adds 2-space indentation. In Python: json.dumps(data, indent=2). In the terminal: cat file.json | python3 -m json.tool or cat file.json | jq .
NDJSON (Newline Delimited JSON) stores one JSON object per line. Each line can be parsed independently, enabling streaming. It is popular for log files, event streams, and large data exports.
Whitespace between tokens is insignificant. {"a":1} and { "a" : 1 } are identical. Formatting is a matter of style and readability, not semantics.
The JSON specification sets no size limit. In practice you are limited by parser memory and network transfer. Most HTTP frameworks impose default body size limits (1–10 MB). For large data, consider streaming parsers or binary formats like MessagePack.
Not directly. JSON is a text format. Binary data (images, files) must be Base64-encoded before embedding in JSON, which increases size by ~33%. For binary-heavy data, consider multipart/form-data or binary formats like CBOR.
JSON5 is a superset of JSON that adds: unquoted keys, single-quoted strings, trailing commas, comments, and multi-line strings. It is easier for humans to write but requires a separate parser. Used in Babel config and some build tooling.