What is JSON?
JSON (JavaScript Object Notation) is a lightweight text format for representing structured data. It was derived from JavaScript object syntax but is now language-independent — every modern programming language has built-in JSON support. JSON has exactly six data types:
- String — text in double quotes:
"hello world" - Number — integer or floating-point:
42,3.14 - Boolean —
trueorfalse - Null — the absence of a value:
null - Object — key-value pairs in curly braces:
{"key": "value"} - Array — ordered list in square brackets:
[1, 2, 3]
Why formatting matters
API responses and stored JSON are typically minified — stripped of all whitespace — to reduce payload size. A minified 50-field API response might be a single horizontal line 2,000 characters long. Formatted with proper indentation, that same response becomes immediately navigable. Formatting is not cosmetic; it is the difference between a debugging session that takes 30 seconds and one that takes 30 minutes.
Beyond readability, formatting tools validate structure. A missing comma, an extra bracket, or a trailing comma (which is valid in JavaScript but not in JSON) will make the formatter report an error with the exact position, saving you from a cryptic parse failure at runtime.
Common JSON errors and how to read them
// Trailing comma — valid in JS, invalid in JSON
{
"name": "Alice",
"age": 30, ← this comma is the error
}
// Single quotes — JSON requires double quotes
{
'name': 'Alice' ← should be "name": "Alice"
}
// Unquoted key — also requires double quotes
{
name: "Alice" ← should be "name": "Alice"
}
// Missing comma between items
{
"name": "Alice" ← comma missing here
"age": 30
}Most parsers report the positionwhere the parsing failed, not necessarily where you made the mistake. If the error says "unexpected token at position 87," the actual error is often the character before position 87 — a missing comma or closing bracket.
Formatting vs minifying
Formatting (also called beautifying or pretty-printing) adds whitespace and newlines to make JSON human-readable. Use this when debugging, reviewing API responses, or reading config files.
Minifying removes all unnecessary whitespace to produce the most compact representation. Use this for production API payloads, localStorage data, and anywhere bandwidth or storage size matters.
// Formatted (32 bytes)
{
"name": "Alice",
"age": 30
}
// Minified (24 bytes — 25% smaller)
{"name":"Alice","age":30}JSON vs XML vs YAML
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Human readability | Good | Verbose | Excellent |
| Comments | No | Yes | Yes |
| Data types | 6 native types | String only (with schemas) | Rich implicit types |
| Primary use | APIs, web | Enterprise, document markup | Config files, DevOps |
| Parse complexity | Simple | Complex | Moderate |
Validating JSON from the terminal
# Validate and pretty-print with jq
cat api-response.json | jq .
# Check if valid (exit code 0 = valid, non-zero = invalid)
cat api-response.json | jq . > /dev/null && echo "Valid" || echo "Invalid"
# Validate with Python (built-in, no install needed)
python3 -m json.tool api-response.json
# Compact output with Python
python3 -c "import json,sys; json.load(sys.stdin)" < api-response.json