JSON Formatting: A Developer's Real-World Guide
Not the beginner tutorial. How JSON formatting decisions affect your team, your APIs, and your debugging workflow — with specific recommendations.
JSON formatting arguments in code reviews waste more time than they're worth. Tabs vs spaces, camelCase vs snake_case, null vs undefined — these shouldn't be debates. Here's what actually matters and what you can safely stop arguing about.
The Basics You Should Have Consistent Across Your API
Key naming convention is the one that bites hardest when it's inconsistent. Pick camelCase or snake_case for your keys and enforce it everywhere. userId vs user_id might look minor, but when a mobile developer is parsing your response and finds both, they write fragile code that breaks in strange ways.
- Key naming: camelCase for JS/TS APIs, snake_case for Python APIs. Pick one.
- Dates: Always ISO 8601. '2025-09-05T14:30:00Z' not '09/05/2025' or 'September 5, 2025'.
- Nulls vs missing keys: Be consistent. Either always include null fields or always omit them. Don't do both.
- Booleans: Never use 0/1 or 'yes'/'no' in place of true/false. Use actual booleans.
- Numbers: Don't wrap numbers in quotes. '42' is wrong; 42 is right.
When to Minify vs When to Pretty-Print
In production APIs: minify. Whitespace adds bytes, bytes add latency, latency hurts users. A 2KB JSON response becomes 1.6KB minified — not huge, but it adds up at scale and gzip will help further.
For config files, storage, and anything humans might read: pretty-print. There's no performance argument for compressing a config file that gets read once at startup. Readability wins.
The Underrated One: Consistent Error Shapes
What breaks API integrations more than formatting is inconsistent error responses. When your success response has {data: {...}} and your error response has {error: 'message'} and sometimes it's {errors: [...]} — every client has to handle three different shapes. Pick one error format and use it everywhere. The RFC 7807 'Problem Details' standard is worth looking at.
Tools to Enforce JSON Style
- ESLint with JSON plugins — enforces key naming, flag issues at lint time
- Prettier — auto-formats JSON files consistently across a codebase
- Our online JSON formatter — good for quick validation and formatting one-offs
- jq — command-line JSON processor, great for scripts and large files
- Spectral — API style linting, goes beyond JSON formatting to API design rules
The Argument That's Actually Worth Having
Pagination format. There's no standard and every team invents theirs. Before bikeshedding about indentation, decide whether your list responses use {data: [], total: 100, page: 1} or {items: [], nextCursor: 'abc'} or {results: [], links: {next: '...'}}. This decision is much harder to change later than two spaces vs four.
Frequently Asked Questions
Should I use 2-space or 4-space indentation for JSON?+
Should JSON keys use camelCase or snake_case?+
Should I include trailing commas in JSON?+
How do I format large JSON files efficiently?+
🔧 Free Tools Used in This Guide
FreeToolKit Team
FreeToolKit Team
We build free browser-based tools and write practical guides that skip the fluff.
Tags: