What is JSON?
A complete guide to JavaScript Object Notation - the most popular data interchange format on the web.
Language Independent
Works with any programming language
Human Readable
Easy to read and write
Lightweight
Minimal syntax overhead
Universal
Standard for web APIs
Introduction to JSON
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format. Despite its name containing "JavaScript," JSON is language-independent and can be used with virtually any programming language.
JSON was derived from JavaScript object literal syntax but has become the de facto standard for data exchange on the web, replacing XML in many applications due to its simplicity and smaller size.
JSON Syntax
JSON is built on two structures:
- Objects: A collection of key/value pairs enclosed in curly braces
{} - Arrays: An ordered list of values enclosed in square brackets
[]
Example JSON Object
{
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"isActive": true,
"address": {
"street": "123 Main St",
"city": "New York"
},
"hobbies": ["reading", "gaming", "cooking"]
}JSON Data Types
JSON supports six data types:
1. String
A sequence of characters enclosed in double quotes. Must use double quotes, not single quotes.
"Hello, World!"
2. Number
Integer or floating-point numbers. No quotes around numbers.
42 3.14159 -17 1.5e10
3. Boolean
Either true or false (lowercase, no quotes).
true false
4. Null
Represents an empty or non-existent value.
null
5. Object
A collection of key/value pairs. Keys must be strings in double quotes.
{
"key1": "value1",
"key2": "value2"
}6. Array
An ordered list of values of any type.
[1, 2, 3, "four", true, null]
JSON Syntax Rules
- Data is in key/value pairs
- Keys must be strings in double quotes
- Values must be a valid JSON data type
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
- No trailing commas allowed
- No comments allowed in standard JSON
Common Mistakes
- Using single quotes: JSON requires double quotes for strings
- Trailing commas: The last item should not have a comma after it
- Unquoted keys: Object keys must be in double quotes
- Comments: JSON does not support comments
- Undefined: JSON does not have an undefined type, use null instead
JSON vs XML
JSON has largely replaced XML for data interchange due to several advantages:
- More compact and readable
- Faster to parse
- Maps directly to data structures in most languages
- Native support in JavaScript
Where JSON is Used
- Web APIs: REST APIs typically return JSON
- Configuration files: package.json, tsconfig.json
- Data storage: NoSQL databases like MongoDB
- Local storage: Browser localStorage and sessionStorage
Try It Yourself
Now that you understand JSON, try our tools to work with JSON data: