If you have spent any time working with REST APIs, you have stared at a wall of minified JSON and wished it would just… breathe. This guide covers practical techniques for reading, debugging, and formatting JSON API responses — from quick browser tricks to command-line tools to purpose-built formatters.
Why JSON Comes Out Ugly
Most APIs return minified JSON by default. Minification removes all whitespace and newlines, reducing response size and improving transfer speed. That is great for production, but genuinely terrible for debugging:
{"user":{"id":42,"name":"Alice","roles":["admin","editor"]},"meta":{"created":"2026-01-15","active":true}}
The same data formatted:
{
"user": {
"id": 42,
"name": "Alice",
"roles": ["admin", "editor"]
},
"meta": {
"created": "2026-01-15",
"active": true
}
}
Same content. Completely different readability.
Method 1: Paste Into a Browser-Based Formatter
The fastest option when you have already copied a JSON string: paste it into an online JSON formatter. A good one will:
- Format and syntax-highlight the JSON instantly
- Validate the structure and show you exactly which line has an error
- Let you minify it back with one click
- Sort keys alphabetically if you need to compare two responses
No installation, no file, no terminal required.
Method 2: Pretty-Print in the Terminal
If you are working with APIs from the command line, jq is the gold standard:
curl -s https://api.example.com/users/42 | jq '.'
The . is the identity filter — it just outputs the entire JSON formatted. jq also lets you extract specific fields:
# Get just the name field
curl -s https://api.example.com/users/42 | jq '.name'
# Get all role names from an array
curl -s https://api.example.com/users/42 | jq '.roles[]'
No jq? Python ships with a built-in JSON formatter:
curl -s https://api.example.com/users/42 | python3 -m json.tool
Method 3: Format JSON in JavaScript
When debugging inside a Node.js script or browser console:
// Pretty-print any object
console.log(JSON.stringify(response, null, 2));
The third argument 2 is the indent level. Use 4 for 4-space indentation, or "\t" for tabs.
To format a JSON string you have already received:
const raw = '{"name":"Alice","age":30}';
const formatted = JSON.stringify(JSON.parse(raw), null, 2);
console.log(formatted);
Method 4: Format JSON in Python
import json
raw = '{"name": "Alice", "age": 30}'
parsed = json.loads(raw)
print(json.dumps(parsed, indent=2))
To sort keys alphabetically (useful for diffs):
print(json.dumps(parsed, indent=2, sort_keys=True))
Debugging Common JSON Errors
"Unexpected token" or "Unexpected end of input"
These usually mean:
-
Trailing comma —
{"a": 1, "b": 2,}— JSON forbids the last comma. JavaScript objects allow it; JSON does not. -
Single quotes — JSON requires double quotes everywhere.
{'name': 'Alice'}is invalid JSON. -
Unquoted keys —
{name: "Alice"}is valid JavaScript but not valid JSON. -
Comments — JSON has no comment syntax.
// this is a commentinside JSON will break parsers.
The easiest fix: paste the JSON into a validator to see the exact line and character position of the error.
The response is valid but nested too deeply
Some APIs return deeply nested objects. A JSON formatter with a collapsible tree view makes it easy to navigate. Alternatively, use jq to drill straight to the value you need:
echo '{"a":{"b":{"c":{"d":"found it"}}}}' | jq '.a.b.c.d'
# "found it"
The response looks right but parsing fails in code
Check the Content-Type header. If the server sends text/plain instead of application/json, fetch() will not automatically parse it. Use response.text() and then JSON.parse() manually:
const text = await response.text();
const data = JSON.parse(text);
Working With Large JSON Responses
For very large responses (tens of MB), browser-based tools may become slow. Use the command line instead:
# Format and pipe through less for navigation
jq '.' large.json | less
# Extract only the fields you care about
jq '[.[] | {id: .id, name: .name}]' large.json > slim.json
Comparing Two JSON Responses
When you need to diff two API responses (before and after a change, or two environments):
# Format both, then diff
jq -S '.' response1.json > a.json
jq -S '.' response2.json > b.json
diff a.json b.json
The -S flag sorts keys, so you compare actual content differences rather than key ordering differences.
Or use the text diff checker to paste both responses and see a side-by-side diff without installing anything.
Summary
| Situation | Best tool |
|---|---|
| Quick one-off format / validate | snappytools.app/json-formatter |
| Command-line API calls | `curl ... \ |
| Inside Node.js or browser console | {% raw %}JSON.stringify(data, null, 2)
|
| Inside Python | json.dumps(data, indent=2) |
| Comparing two responses |
jq -S + diff, or text diff tool |
| Very large files |
jq + less
|
JSON is simple in theory and occasionally annoying in practice. Having the right tool for each situation takes the friction out of debugging.
Top comments (0)