What is JSON? A Complete Guide for Developers
What is JSON?
JSON stands for JavaScript Object Notation. It 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. Despite having "JavaScript" in its name, JSON is a language-independent format. Virtually every modern programming language — including Python, Java, Go, C#, Ruby, PHP, Swift, and Rust — provides built-in or first-class support for reading and writing JSON.
Because JSON is a plain-text format, it is ideal for transmitting data over a network and for storing structured information in files. Today, JSON is the most widely used data interchange format in web development and has become the de facto standard response format for REST APIs. If you have ever called a web API or opened a package.json file, you have already worked with JSON.
A Brief History of JSON
JSON was first proposed by Douglas Crockford in the early 2000s. At the time, XML was the dominant format for exchanging data between servers and clients in web applications. However, XML's tag-heavy syntax resulted in verbose payloads and complex parsing logic.
Crockford drew inspiration from the object literal syntax in JavaScript and designed a far more concise and efficient data format. JSON was first formally standardized in 2006 as RFC 4627. The specification was later revised in 2017 as RFC 8259, which remains the current Internet standard. JSON is also standardized as ECMA-404, giving it recognition as an international standard maintained by ECMA International.
The simplicity of JSON was a key factor in its rapid adoption. Unlike XML, which requires a specialized parser and schema definitions, JSON maps directly to the native data structures of most programming languages — objects (or dictionaries/maps) and arrays (or lists). This natural mapping made JSON an instant hit among developers building AJAX-powered web applications in the mid-2000s, and its popularity has only grown since.
JSON Syntax Basics
The fundamental building block of JSON is the key-value pair. Keys must always be strings enclosed in double quotes, while values can be any of the supported JSON data types. Curly braces {} denote objects, and square brackets [] denote arrays.
Objects
A JSON object is an unordered collection of key-value pairs wrapped in curly braces. Each pair is separated by a comma, and the key is separated from its value by a colon.
{
"name": "John Doe",
"age": 30,
"email": "john@example.com"
}Arrays
A JSON array is an ordered list of values wrapped in square brackets. Values within an array are separated by commas and can be of different types.
{
"fruits": ["apple", "banana", "grape"],
"mixed": [1, "hello", true, null]
}Nested Structures
JSON supports nesting objects within objects and arrays within arrays, allowing you to represent complex, hierarchical data structures.
{
"user": {
"name": "John Doe",
"address": {
"city": "New York",
"zipcode": "10001"
},
"hobbies": ["reading", "coding", "hiking"]
}
}JSON Data Types
JSON supports exactly six data types. Understanding each type and its rules is essential for writing valid JSON and for correctly parsing JSON data in your applications.
1. String
A string is a sequence of zero or more Unicode characters wrapped in double quotes. Single quotes are not allowed in JSON. You can use escape sequences such as \n (newline),\t (tab), \\ (backslash), and \" (double quote) within strings.
{
"greeting": "Hello, World!",
"path": "C:\\Users\\Documents",
"quote": "He said \"hello\""
}2. Number
JSON numbers can be integers or floating-point values. Exponential (scientific) notation is also supported. However, special values like NaN and Infinity are not valid in JSON. Numbers must not have leading zeros (except for the value 0 itself or values between -1 and 1 like 0.5).
{
"integer": 42,
"negative": -17,
"float": 3.14,
"exponent": 2.5e10
}3. Boolean
A boolean value is either true or false. These must be written in lowercase without any quotes.
{
"isActive": true,
"isDeleted": false
}4. Null
The null type represents an intentionally empty or absent value. It must be written in lowercase without quotes.
{
"middleName": null,
"deletedAt": null
}5. Object
An object is an unordered collection of key-value pairs wrapped in curly braces. Values can be any valid JSON data type, including other objects, making recursive nesting possible.
{
"database": {
"host": "localhost",
"port": 5432,
"name": "mydb"
}
}6. Array
An array is an ordered list of values wrapped in square brackets. Arrays can contain values of mixed types and can be nested within each other.
{
"scores": [95, 87, 92, 78],
"matrix": [[1, 2], [3, 4], [5, 6]]
}JSON vs XML
JSON and XML are both text-based formats used for data interchange, but they differ in several important ways. Here is a detailed comparison:
- Readability: JSON has a concise, clean syntax that is easy for humans to read. XML uses opening and closing tags, which results in more verbose and repetitive markup.
- Data size: For the same data, JSON is typically 30-50% smaller than XML because it avoids the overhead of repeated tag names.
- Parsing speed: Most programming languages provide native JSON parsers that are faster than XML parsers. JSON maps directly to hash maps and arrays, while XML requires DOM or SAX parsing.
- Array support: JSON has native support for arrays as a first-class data type. In XML, arrays must be represented using repeated child elements, which is less intuitive.
- Comments: XML supports comments (
<!-- comment -->), but JSON does not allow comments of any kind. - Namespaces: XML supports namespaces, which are useful for defining complex document structures with elements from multiple vocabularies. JSON has no equivalent feature.
- Schema and validation: XML has a mature ecosystem of schema languages (XSD, DTD, Relax NG). JSON Schema has matured significantly but is still younger than its XML counterparts.
The difference becomes clear when you compare the same data represented in both formats:
// JSON
{
"employees": [
{ "name": "Alice", "age": 30 },
{ "name": "Bob", "age": 25 }
]
}
<!-- XML -->
<employees>
<employee>
<name>Alice</name>
<age>30</age>
</employee>
<employee>
<name>Bob</name>
<age>25</age>
</employee>
</employees>As you can see, the JSON version is significantly more compact. For most modern web applications, JSON is the preferred choice due to its simplicity and smaller payload size.
Common Use Cases
JSON is used as a core data format across a wide range of domains in modern software development.
REST APIs
The vast majority of modern web services exchange data in JSON format through REST APIs. Major APIs such as GitHub, Stripe, Twitter, Google Maps, and OpenAI all use JSON as their primary request and response format. When you make an API call, the response you receive is almost always a JSON object.
// Example: GitHub API response
{
"id": 1,
"login": "octocat",
"name": "The Octocat",
"public_repos": 8,
"followers": 20000
}Configuration Files
Many development tools and frameworks use JSON for their configuration files. Common examples include package.json (Node.js project metadata and dependencies), tsconfig.json (TypeScript compiler options), .eslintrc.json (ESLint rules), and composer.json (PHP dependencies). These files are easy to read, edit, and parse programmatically.
Data Exchange Between Services
In microservice architectures, JSON is the standard format for inter-service communication. Message queues such as Apache Kafka, RabbitMQ, and Amazon SQS commonly carry JSON-encoded payloads for event streaming and asynchronous processing.
NoSQL Databases
Document-oriented NoSQL databases like MongoDB, CouchDB, and Firebase Firestore store and query data in JSON or JSON-like formats (such as BSON in MongoDB). This makes it natural to work with JSON data end-to-end — from the database layer through the API to the frontend.
Common Mistakes When Writing JSON
Even experienced developers occasionally make mistakes when writing JSON by hand. Being aware of these common pitfalls can save you significant debugging time.
1. Trailing Commas
In JavaScript, trailing commas after the last element of an object or array are perfectly valid. In JSON, however, trailing commas are not allowed and will cause a parsing error.
// Invalid JSON - trailing comma
{
"name": "John",
"age": 30,
}
// Valid JSON
{
"name": "John",
"age": 30
}2. Single Quotes
JSON requires all strings and keys to be enclosed in double quotes. Single quotes are not valid in JSON, even though many programming languages accept them for strings.
// Invalid JSON - single quotes
{ 'name': 'John' }
// Valid JSON - double quotes
{ "name": "John" }3. Comments Are Not Allowed
JSON does not support comments. Including // or /* */ style comments in a JSON file will result in a parsing error. If you need comments in your configuration files, consider using JSONC (JSON with Comments) or JSON5, which are supersets of JSON that allow comments.
4. Unquoted Keys
All keys in JSON must be strings wrapped in double quotes. Unquoted keys (which are valid in JavaScript object literals) will cause a parsing error in JSON.
// Invalid JSON - unquoted keys
{ name: "John", age: 30 }
// Valid JSON - quoted keys
{ "name": "John", "age": 30 }5. Other Pitfalls to Watch For
undefinedis not a valid JSON value — usenullinstead- JSON has no native date type — use ISO 8601 formatted strings (e.g.,
"2024-01-15T10:30:00Z") - Numeric keys must still be wrapped in quotes (e.g.,
{ "1": "one" }not{ 1: "one" }) - UTF-8 encoding is recommended for maximum compatibility
- Duplicate keys are technically allowed by the spec but discouraged — behavior varies across parsers
Conclusion
JSON is an indispensable data format in modern software development. Its concise syntax, excellent readability, and universal language support have made it the go-to choice for REST APIs, configuration files, data storage, inter-service communication, and much more. By mastering JSON's basic syntax, understanding its six data types, and being aware of common pitfalls, you can work more efficiently and avoid frustrating debugging sessions.
Whether you are building a web application, designing an API, or configuring a development tool, JSON will almost certainly be part of your workflow. The more comfortable you are with it, the more productive you will be.
Ready to format and validate your own JSON data? Try the tool below.
JSON Formatter — Format, validate, sort, and minify your JSON right in the browser. No installation required.