What is JSON? — JavaScript Object Notation Explained
Definition
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that uses human-readable text to store and transmit data objects consisting of attribute-value pairs and arrays. It is language-independent, though it uses conventions familiar to C-family languages including C, C++, C#, Java, JavaScript, Perl, Python, and many others. JSON is commonly used for asynchronous browser-server communication, replacing XML in many web applications.
JSON Syntax Rules
JSON syntax is derived from JavaScript object notation syntax, but the format is text only:
- Data is in name/value pairs
- Data is separated by commas
- Curly braces
{}hold objects - Square brackets
[]hold arrays - Keys must be double-quoted strings
- Values must be one of the supported data types
- Trailing commas are not allowed
JSON Data Types
| Type | Example | Description |
|---|---|---|
| String | "Hello World" |
Double-quoted Unicode text |
| Number | 42, 3.14, -7 |
Integer or floating point |
| Boolean | true, false |
Logical true/false |
| Null | null |
Empty or non-existent value |
| Object | {"key": "value"} |
Unordered collection of key/value pairs |
| Array | [1, 2, 3] |
Ordered list of values |
Simple JSON Example
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}Nested Structures
JSON supports deep nesting, allowing you to represent complex relationships:
{
"company": {
"name": "Tech Corp",
"departments": [
{
"name": "Engineering",
"employees": [
{"name": "Alice", "role": "Senior Dev"},
{"name": "Bob", "role": "Junior Dev"}
]
},
{
"name": "Design",
"employees": [
{"name": "Charlie", "role": "Designer"}
]
}
]
}
}JSON vs XML
| Aspect | JSON | XML |
|---|---|---|
| Syntax | Compact, object-like | Verbose, tag-based |
| Data Types | Native types | All text |
| Parsing | Simple | Complex (DOM/SAX) |
| Browser Support | Native | Requires parser |
| Comments | Not supported | Supported |
| Attributes | Not supported | Supported |
| Use Case | APIs, web apps | Enterprise, documents |
Common JSON Use Cases
Web APIs
JSON is the standard format for REST and GraphQL APIs. Almost every public API returns JSON.
Configuration Files
Many tools and frameworks use JSON for configuration (package.json, tsconfig.json, .eslintrc.json).
NoSQL Databases
MongoDB, CouchDB, and Firebase store data in JSON-like formats (BSON for MongoDB).
Data Storage
JSON is used for serializing structured data in files, local storage, and caches.
Cross-Language Communication
JSON bridges different programming languages in microservice architectures.
LangStop JSON Tools
- JSON Formatter — Pretty print and beautify
- JSON Validator — Check syntax
- JSON Minifier — Compress JSON
- JSON Diff — Compare JSON objects
- JSON to YAML — Convert to YAML
- JSON to XML — Convert to XML
- JSON to CSV — Convert to CSV
- JSONPath Explorer — Query JSON data