JSON Formatting: A Complete Guide for Developers
What JSON Actually Is
JSON (JavaScript Object Notation) is a text-based data interchange format. Despite the name, it is language-independent — every modern programming language can parse and generate JSON. It became the default format for web APIs because it is human-readable, compact, and maps naturally to the data structures programmers already use: objects, arrays, strings, numbers, booleans, and null.
The specification is remarkably small. The entire grammar fits on a single page at json.org. This simplicity is its greatest strength.
The Formatting Problem
JSON that comes from APIs, logs, or minified files is usually compressed into a single line:
{"users":[{"id":1,"name":"Alice","roles":["admin","editor"]},{"id":2,"name":"Bob","roles":["viewer"]}],"total":2}
This is valid JSON but impossible to read. Formatting (also called "pretty printing") adds indentation and line breaks:
{
"users": [
{
"id": 1,
"name": "Alice",
"roles": ["admin", "editor"]
},
{
"id": 2,
"name": "Bob",
"roles": ["viewer"]
}
],
"total": 2
}
Same data, completely different readability. A good JSON formatter does this instantly.
Common JSON Mistakes
Trailing Commas
{
"name": "Alice",
"age": 30,
}
That comma after 30 is invalid JSON. JavaScript allows trailing commas in objects and arrays, but JSON does not. This is the single most common JSON error.
Single Quotes
{'name': 'Alice'}
JSON requires double quotes for both keys and string values. Single quotes are not valid. Python developers hit this constantly when converting dictionaries to JSON.
Unquoted Keys
{name: "Alice"}
Every key must be a double-quoted string. No exceptions.
Comments
{
// This is not valid
"name": "Alice"
}
JSON has no comment syntax. If you need commented configuration files, consider JSONC (JSON with Comments, supported by VS Code) or YAML. But standard JSON parsers will reject comments.
Numbers with Leading Zeros
{"code": 007}
Leading zeros are not valid in JSON numbers. Use 7 or the string "007".
Validation vs. Formatting
Formatting makes valid JSON readable. Validation checks whether a string is valid JSON at all. A good tool does both simultaneously — the ToolFlip JSON formatter highlights syntax errors with line numbers and descriptions while formatting valid input.
Common validation checks:
- Balanced braces and brackets
- Proper comma placement (no trailing commas, no missing commas)
- All strings use double quotes
- No duplicate keys (technically valid per spec, but usually a bug)
- Valid escape sequences in strings
Working with JSON in the Terminal
Format JSON from the command line with jq:
# Pretty print a file
cat data.json | jq .
# Pretty print an API response
curl -s https://api.example.com/users | jq .
# Extract a specific field
cat data.json | jq '.users[0].name'
Python's built-in module works too:
python3 -m json.tool data.json
For quick one-off formatting without installing anything, paste into an online JSON formatter.
JSON vs. Other Formats
| Feature | JSON | YAML | XML | TOML | |---|---|---|---|---| | Human readable | Good | Best | Worst | Good | | Comments | No | Yes | Yes | Yes | | Data types | 6 | Many | Text only | Rich | | Parsing speed | Fast | Slow | Medium | Fast | | File size | Medium | Smallest | Largest | Small | | API standard | Yes | Rare | Legacy | Config only |
JSON wins for APIs and data exchange. YAML wins for configuration files. XML survives in legacy systems and some document formats. TOML is gaining ground for application config.
Converting JSON to Other Formats
Need to analyze JSON data in a spreadsheet? Convert it to CSV with a JSON to CSV converter. Working with JWTs? The payload is Base64-encoded JSON — a JWT decoder extracts and formats it automatically.
Performance Considerations
For large JSON files (10MB+), browser-based formatters may slow down. Strategies:
- Stream processing: Use
jqfor files that do not need to fit in memory - Partial formatting: Extract the section you need with
jqbefore formatting - Binary alternatives: Consider MessagePack, BSON, or Protocol Buffers for performance-critical applications
For files under 10MB, a browser-based JSON formatter handles formatting and validation instantly with no server uploads — your data stays on your device.
Best Practices
-
Always validate before debugging. If your API returns unexpected results, paste the response into a validator first. Half the time, the JSON itself is malformed.
-
Use consistent indentation. Two spaces is the most common convention. Four spaces works too. Tabs are rare in JSON. Pick one and stick with it.
-
Keep nesting shallow. If your JSON is more than 4-5 levels deep, consider flattening the structure. Deep nesting makes both parsing and debugging harder.
-
Use arrays for ordered collections, objects for named properties. This seems obvious, but mixing them up is a common design mistake in API responses.
-
Validate in CI/CD. If your application reads JSON config files, add a validation step to your pipeline. Catching a missing comma before deployment is much better than catching it in production.