JSON Syntax Rules
- Data is in key-value pairs:
"key": "value" - Keys must be strings in double quotes
- Values can be: string, number, boolean (true/false), null, object {}, or array []
- No trailing commas after the last item
- No comments (unlike JavaScript)
- No single quotes—only double quotes for strings
Common JSON Errors
| Error | Wrong | Correct |
|---|---|---|
| Single quotes | {'name': 'John'} | {"name": "John"} |
| Trailing comma | {"a": 1, "b": 2,} | {"a": 1, "b": 2} |
| Unquoted key | {name: "John"} | {"name": "John"} |
| undefined | {"val": undefined} | {"val": null} |
JSON vs Other Formats
JSON vs XML: JSON is lighter, easier to parse, and has become the standard for web APIs. XML supports comments, attributes, and schemas but is more verbose.
JSON vs YAML: YAML is more human-readable (no brackets, uses indentation) and supports comments. Used for config files (Docker Compose, Kubernetes). JSON is better for data exchange because it's unambiguous.