JSON Best Practices for Maintainable APIs and Configs
Field-tested JSON best practices covering naming, nulls, versioning, size, security, and documentation habits.
Practice beats cleverness
JSON’s simplicity invites inconsistency. Best practices exist to keep payloads predictable across teams and years. Adopt conventions early; changing field names later is expensive.
Prefer clear, stable naming
Use consistent casing—camelCase is common in JavaScript APIs; snake_case appears in many Python services. Pick one per public API surface. Name booleans affirmatively (isActive, done) and avoid abbreviations that only make sense inside one team chat.
Avoid reusing the same key for different types across versions. If id was a number and becomes a string, that is a breaking change—version it.
Be explicit about null and omission
Document whether omitted keys and null mean different things. Clearing a profile field with null is a useful PATCH convention—if and only if clients know about it. Do not silently coerce missing values to empty strings unless that is a deliberate domain rule.
Keep arrays arrays
Return [] for empty collections, not null. Clients hate null-checking every list. Reserve null for genuinely unknown or cleared scalars when needed.
Limit surprise depth and size
Deep trees are hard to query and easy to DoS. Flatten when possible. Paginate lists. Cap request body sizes. Reject unexpected nesting depth on public endpoints.
Canonicalize what you store
For signed payloads or cache keys, define canonical JSON rules (key order, number formatting). Ordinary APIs can preserve serializer defaults, but cryptographic and diff-sensitive contexts need determinism.
Separate pretty from wire format
Store human-edited configs pretty-printed. Emit minified JSON on the wire. Do not debate whitespace in runtime responses—set JSON.stringify spacing to none in production.
Validate at boundaries
Parse, then validate schema, then map to domain types. Log validation failures with request IDs, not raw secrets. During design, inspect examples with a formatter so humans and schemas agree—JSON Editor is a convenient local companion for that review.
Security-conscious JSON
- Never put passwords or raw session tokens in logs of JSON bodies
- Be careful with
__proto__and polluted keys in certain JavaScript merge utilities - Escape appropriately when embedding JSON inside HTML
- Treat JSON from the browser as hostile until proven otherwise
Documentation that stays true
Publish examples that CI validates against schemas. Include both success and error bodies. Note content types and character encoding (UTF-8). When you deprecate a field, mark it in docs and keep it until a version bump removes it.
Review checklist for PRs that touch JSON
- Naming matches existing resources
- Empty lists are
[] - Errors use a stable structure
- Examples still parse
- No accidental PII in fixtures
- Size and pagination considered for list endpoints
The payoff
Consistent JSON reduces client bugs, shortens onboarding, and makes observability saner. None of these practices require exotic tooling—only discipline. Start with naming, null policy, and validation; add sophistication only where risk justifies it.
Related articles
Guides · 3 min read
JSON Parsing Guide: From Text to Typed Data Safely
How JSON parsing works across languages, common pitfalls with numbers and dates, and safer patterns for production code.
APIs · 3 min read
REST API JSON Basics: Methods, Status Codes, and Payloads
Learn how REST-style HTTP APIs use JSON for bodies, which status codes pair with which outcomes, and how to avoid common pitfalls.
APIs · 3 min read
JSON API Tutorial: Designing Clean Request and Response Bodies
Practical patterns for JSON APIs including envelopes, errors, pagination, versioning, and examples you can adapt.