JSON (JavaScript Object Notation) is a lightweight, text-based format used to structure and exchange data between a server and a client, between two services, or between programs written in entirely different languages. It is built on two fundamental structures: a collection of key/value pairs (known as an object) and an ordered list of values (known as an array). Originally derived from a subset of JavaScript's object literal syntax, JSON is now completely language-independent and supported by virtually every modern programming language, from Python and Java to Go, Rust, and PHP.
Because of its simplicity and predictability, JSON has become the default format for REST APIs, configuration files, NoSQL databases, and countless developer tools. If you have ever inspected a network request in your browser's DevTools, read a package.json file, or called a public API, you have worked with JSON.
Why is JSON so popular?
- Human-Readable: Its syntax is clean and easy to understand, which simplifies development and debugging.
- Machine-Friendly: It is trivial for machines to parse and generate, making it efficient for data exchange.
- Wide Adoption: It has become the de facto standard for APIs, configuration files, and data storage on the web.
- Language-Independent: Nearly every programming language ships with a built-in or widely used library for parsing and generating JSON.
- Compact: Compared to XML, JSON has far less markup overhead, resulting in smaller payloads over the network.
Basic JSON Example
{
"user": {
"name": "John Doe",
"id": 12345,
"isVerified": true,
"roles": ["admin", "editor"],
"profile": null
}
}JSON Data Types
JSON supports only a small, fixed set of data types. This simplicity is part of why it's so easy to parse consistently across languages.
- String: Text wrapped in double quotes, e.g.
"hello". - Number: Integers or floating-point numbers, e.g.
42or3.14. JSON has no separate type for integers vs. decimals. - Boolean: Either
trueorfalse. - Null: Represents an empty or missing value, written as
null. - Object: An unordered set of key/value pairs wrapped in curly braces
{ }. - Array: An ordered list of values wrapped in square brackets
[ ].
JSON Syntax Rules
JSON's grammar is intentionally strict, which is what makes it so reliable to parse. A few rules trip up beginners most often:
- Keys must always be strings wrapped in double quotes.
- Single quotes are not valid — only double quotes are allowed for strings and keys.
- Trailing commas after the last item in an object or array are not allowed.
- Comments are not part of the JSON specification.
- Values can be strings, numbers, booleans, null, objects, or arrays — but not functions, dates, or
undefined.
Common JSON Mistakes
{
'name': "Invalid", // single quotes not allowed
"age": 30,
"tags": ["a", "b",] // trailing comma not allowed
}If your JSON fails to parse, the culprit is usually a trailing comma, an unquoted key, a stray comment, or mismatched brackets/braces. A validator or formatter can catch these instantly.
JSON vs. XML vs. YAML
JSON is often compared to XML and YAML, since all three are used to structure and exchange data:
- JSON vs. XML: JSON has less syntactic overhead (no closing tags), maps naturally to native data structures in most languages, and is generally faster to parse. XML supports attributes, namespaces, and schema validation more richly, which is why it's still common in enterprise and document-centric systems.
- JSON vs. YAML: YAML is more human-friendly for hand-written configuration files since it relies on indentation rather than braces and brackets. However, YAML's flexible syntax can introduce subtle parsing ambiguities, while JSON's stricter grammar makes it more predictable for machine-to-machine communication.
Where JSON Is Used
- APIs: Most REST and many GraphQL APIs send and receive data as JSON.
- Configuration files: Tools like npm (
package.json), TypeScript (tsconfig.json), and VS Code use JSON for settings. - NoSQL databases: Databases such as MongoDB store documents in a JSON-like binary format (BSON).
- Data interchange: Logs, message queues, and webhooks frequently use JSON as their payload format.
- Local storage: Web browsers commonly serialize objects to JSON strings for storage in
localStorageor cookies.
Parsing and Generating JSON
Most languages provide built-in functions to convert between JSON text and native data structures. In JavaScript, for example:
// Convert a JSON string into a JavaScript object
const obj = JSON.parse('{"name":"John Doe","id":12345}');
// Convert a JavaScript object into a JSON string
const jsonString = JSON.stringify(obj, null, 2);This process is often called serialization (object → JSON string) and deserialization or parsing (JSON string → object).
Frequently Asked Questions
Is JSON a programming language? No. JSON is a data format, not a language. It has no logic, functions, or loops — only data.
Can JSON have comments? No. The JSON specification does not support comments. Some tools support a relaxed variant called JSON5 or JSONC that does, but standard JSON does not.
What file extension does JSON use? JSON files use the .json extension and the MIME type application/json.
{
"user": {
"name": "John Doe",
"id": 12345,
"isVerified": true,
"roles": ["admin", "editor"],
"profile": null
}
}