What is JSON? A Practical Guide for Developers
Learn what JSON is, why it's the universal data format, and how to use it effectively.
JSON (JavaScript Object Notation) is the most widely used data format for APIs, configuration files, and web applications. Despite the name, JSON works across all programming languages.
Why JSON Matters
JSON has become the de facto standard because it is:
- **Human-readable**: Unlike binary formats, you can read and edit JSON directly
- **Machine-efficient**: Parsers are fast and built into every modern language
- **Universal**: Supported everywhere—browsers, servers, databases, mobile apps
- **Simple**: A developer can learn JSON in minutes
Every REST API returns JSON. Every configuration framework accepts JSON. Every NoSQL database stores JSON. If you work with data on the internet, you work with JSON daily.
JSON Structure
JSON consists of two basic building blocks:
**Objects** are key-value pairs in curly braces:
{
"name": "Alice",
"age": 28,
"email": "alice@example.com"
}
**Arrays** are ordered lists in square brackets:
["apple", "banana", "cherry"]
You can nest objects and arrays to create complex structures:
{
"users": [
{
"id": 1,
"name": "Alice",
"tags": ["admin", "developer"]
},
{
"id": 2,
"name": "Bob",
"tags": ["user"]
}
]
}
Data Types in JSON
JSON supports five primitive types:
- **String**: "hello", "alice@example.com" (must use double quotes)
- **Number**: 42, 3.14, -17 (integers or floats)
- **Boolean**: true or false (lowercase)
- **Null**: null (represents missing or empty value)
- **Object or Array**: nested structures
Everything else must be represented as one of these types. There is no date type, so dates are usually strings ("2024-01-15") or Unix timestamps (1705276800).
JSON Rules
JSON has strict rules. Breaking them causes parsing errors:
- Keys must be double-quoted: "name", not 'name' or name
- Strings must use double quotes: "hello", not 'hello'
- No trailing commas: ["a", "b"] not ["a", "b",]
- No comments: JSON ignores // and /* */
- No single quotes: {"key": 'value'} is invalid
- No undefined: use null instead
These rules exist because JSON must be parseable by machines. Flexibility would make parsing impossible.
When to Use JSON
Use JSON when you need:
- **APIs**: All modern REST APIs return JSON
- **Configuration**: app.json, package.json, tsconfig.json
- **Logging**: Structured logs are often JSON
- **Storage**: NoSQL databases like MongoDB store JSON
- **Data interchange**: Between different systems and languages
JSON vs Other Formats
**JSON vs XML**: JSON is simpler and smaller. XML is more verbose but allows custom tags. Use JSON unless you specifically need XML's features.
**JSON vs YAML**: YAML is more human-friendly (no quotes required) but JSON is stricter and more widely supported. Use JSON for APIs, YAML for config files.
**JSON vs CSV**: CSV is good for tabular data. JSON is better for complex structures. Most tools can convert between them.
Common JSON Mistakes
- Forgetting double quotes on keys or string values
- Adding trailing commas in objects or arrays
- Using single quotes instead of double quotes
- Including comments (JSON spec does not allow them)
- Using undefined instead of null
Validating and Formatting JSON
When JSON breaks, it's usually a simple syntax error. Use the DevToolkit JSON Validator to find errors instantly. The JSON Formatter helps you read large JSON documents by adding indentation.
Best Practices
- Use meaningful key names: "user_email" not "ue"
- Be consistent: use camelCase, snake_case, or kebab-case, but pick one
- Flatten deeply nested structures if possible for readability
- Use null for missing values, not empty strings
- Document your JSON schema so others understand its structure
JSON is simple, powerful, and everywhere. Understanding it well makes you a better developer.