# JavaScript Object Notation (JSON)
JSON (JavaScript Object Notation) is a lightweight data interchange format specified and popularized by [[Douglas Crockford]] in the early 2000s. Despite its name and JavaScript origins, JSON is language-independent and has become the dominant format for data exchange on the web, APIs, and configuration files.
Crockford discovered that a subset of JavaScript's object literal notation made an excellent data interchange format. JSON's simplicity and readability made it the successor to XML for most web applications. It's human-readable, easy to parse, and supported by virtually every programming language.
## Key Characteristics
- **Text-based**: Plain text format, human-readable
- **Language-independent**: Works with any programming language
- **Lightweight**: Minimal syntax overhead
- **Self-describing**: Structure is embedded in the data
- **Hierarchical**: Supports nested structures
## Data Types
JSON supports six data types:
| Type | Example |
|------|---------|
| String | `"hello"` |
| Number | `42`, `3.14`, `-17` |
| Boolean | `true`, `false` |
| Null | `null` |
| Array | `[1, 2, 3]` |
| Object | `{"key": "value"}` |
## Syntax Rules
- Data is in key/value pairs
- Keys must be strings (double quotes)
- Data is separated by commas
- Objects use curly braces `{}`
- Arrays use square brackets `[]`
- No trailing commas allowed
- No comments (by specification)
## Example
```json
{
"name": "Sébastien",
"age": 41,
"isCreator": true,
"skills": ["PKM", "Writing", "Software Development"],
"contact": {
"email": "
[email protected]",
"website": "https://dsebastien.net"
},
"nickname": null
}
```
## Common Use Cases
- **Web APIs**: REST and GraphQL responses
- **Configuration files**: `package.json`, `tsconfig.json`
- **Data storage**: NoSQL databases (MongoDB, CouchDB)
- **Data exchange**: Between services and applications
- **Logging**: Structured log formats
## JSON vs Alternatives
| Format | Strengths | Weaknesses |
|--------|-----------|------------|
| JSON | Simple, ubiquitous, readable | No comments, verbose |
| YAML | Comments, less verbose | Whitespace-sensitive, complex |
| XML | Schema validation, attributes | Verbose, harder to read |
| TOML | Comments, sections | Less common, flat structure |
## Working with JSON
### Command Line
Use [[jq]] for processing JSON in the terminal:
```bash
cat data.json | jq '.name'
curl api.example.com | jq '.[0]'
```
### JavaScript
```javascript
// Parse JSON string
const obj = JSON.parse('{"name": "value"}');
// Convert to JSON string
const json = JSON.stringify(obj, null, 2);
```
### Python
```python
import json
# Parse
data = json.loads('{"name": "value"}')
# Stringify
json_str = json.dumps(data, indent=2)
```
## JSON Schema
JSON Schema is a vocabulary for validating JSON documents:
```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"]
}
```
## References
- Official specification: https://www.json.org/
- JSON Schema: https://json-schema.org/
- https://en.wikipedia.org/wiki/JSON
## Related
- [[Douglas Crockford]]
- [[JSONL]]
- [[jq]]
- [[Yet Another Markup Language (YAML)]]