If you work with APIs, you work with JSON. And if you have ever spent 20 minutes hunting for a missing comma in a 500-line JSON response, this guide is for you.
What is JSON?
JSON (JavaScript Object Notation) is the de facto data interchange format for web APIs. It is human-readable, lightweight, and supported by every programming language.
{
"name": "The Apps Firm",
"tools": 48,
"free": true,
"categories": ["formatters", "generators", "calculators"]
}
Common JSON Mistakes
1. Trailing commas
JSON does not allow trailing commas. JavaScript does, which causes confusion when copying objects between JS and JSON.
2. Single quotes
JSON requires double quotes for all strings and keys. Single quotes are invalid.
3. Unquoted keys
Unlike JavaScript objects, JSON keys must always be quoted strings.
4. Comments
JSON does not support comments. Use JSONC or JSON5 if you need them.
How to Format JSON
Command line:
python3 -m json.tool data.json
curl -s https://api.example.com/data | jq .
In your editor:
- VS Code: Select JSON, press Shift+Alt+F
- Vim: :%!python3 -m json.tool
Online:
I built a free JSON formatter that does formatting, validation, minification, tree view, and JSON-to-YAML/CSV conversion entirely in your browser.
How to Validate JSON
function isValidJSON(str) {
try { JSON.parse(str); return true; }
catch (e) { return false; }
}
JSON vs YAML vs XML
| Feature | JSON | YAML | XML |
|---|---|---|---|
| Readability | Good | Best | Worst |
| Comments | No | Yes | Yes |
| File size | Small | Smallest | Largest |
| Parsing speed | Fast | Slow | Slow |
| Use case | APIs, config | Config, K8s | Legacy, SOAP |
Useful JSON Tools
- jq - command-line JSON processor
- JSON Formatter - online formatter with syntax highlighting
- JSON/CSV Converter - convert between formats
Pro Tips
- Always validate API responses before parsing
- Use JSON.stringify(obj, null, 2) for readable logging
- Handle BigInt - JSON.parse cannot handle numbers larger than 2^53
- Use streaming parsers for large files
- Sanitize before parsing to prevent prototype pollution
What JSON tools do you use daily? Share in the comments.
Top comments (0)