JSON (JavaScript Object Notation) has become the universal language of data exchange on the web. Nearly every REST API, configuration file, and data pipeline uses JSON. Despite its simplicity, there are many ways to write JSON well or poorly. This guide covers the most important JSON best practices that will make your APIs cleaner, your code more maintainable, and your systems more performant.
1. Use Consistent Naming Conventions
The most common source of confusion in JSON APIs is inconsistent key naming. Pick a convention and stick to it throughout your entire API:
- camelCase (
firstName,userId) — Preferred in JavaScript and most web APIs (including Google, Stripe, and GitHub APIs). - snake_case (
first_name,user_id) — Common in Python and Ruby APIs, and many database-driven systems. - kebab-case (
first-name) — Rarely used in JSON as hyphens require quoted keys in JavaScript.
Whatever you choose, apply it universally. Mixing user_id and userId in the same API is one of the most frustrating inconsistencies a consumer can encounter.
2. Always Use Double Quotes for Keys and String Values
The JSON specification (RFC 8259) requires that keys and string values be enclosed in double quotes. Single quotes are not valid JSON. This is a common gotcha when converting JavaScript object literals to JSON, since JavaScript itself allows single quotes.
{'name': 'Pan Tool', 'free': true}
{"name": "Pan Tool", "free": true}
3. Use Appropriate Data Types
JSON supports six data types: string, number, boolean, null, array, and object. Use the type that semantically matches the data — don't use strings for everything.
- Use boolean
true/false, not strings"true"/"false"or numbers1/0for boolean values. - Use null for the intentional absence of a value. Don't use empty strings
""or0to represent "no value". - Use numbers for numeric quantities, not strings like
"42". - Use arrays for ordered collections of the same type of item.
- Use ISO 8601 strings for dates:
"2025-04-15T10:30:00Z". JSON has no native date type.
4. Design a Consistent Response Envelope
For REST APIs, always wrap your response in a consistent envelope structure. This makes error handling predictable for consumers:
{
"success": true,
"data": {
"id": 42,
"name": "Pan Tool"
},
"meta": {
"total": 1,
"page": 1
}
}
// On error:
{
"success": false,
"error": {
"code": "NOT_FOUND",
"message": "The requested resource was not found."
}
}
Consistent envelopes mean your API consumers can write a single response-handling utility rather than dealing with different response shapes for every endpoint.
5. Validate JSON Before Processing
Never assume that incoming JSON is valid. Always validate both the syntax (is it valid JSON?) and the structure (does it contain the fields you expect?) before processing.
- In PHP, check
json_last_error() === JSON_ERROR_NONEafter callingjson_decode(). - In JavaScript/Node.js, wrap
JSON.parse()in atry/catch. - Use JSON Schema validation libraries to validate structure, types, and required fields systematically.
6. Minify JSON for Production, Beautify for Development
In development and in version-controlled configuration files, use prettified JSON with indentation for readability. In production API responses, serve minified JSON to reduce payload size.
A well-formatted JSON configuration file with 500 lines might be 20KB. The minified equivalent might be 12KB — a 40% reduction. When served over HTTPS with GZIP compression, minified JSON compresses even better than formatted JSON.
Use our free JSON Minifier to compress JSON for production, and our JSON Beautifier to expand minified JSON for reading and editing.
7. Avoid Deeply Nested Structures
Deeply nested JSON is hard to read, hard to query, and often a sign of poor data modeling. As a general rule, try to keep your JSON depth to 3–4 levels maximum. If you find yourself nesting objects 6 or 7 levels deep, it's usually a signal to reconsider your data model.
{
"user": {
"profile": {
"address": {
"billing": {
"country": { "code": "US" }
}
}
}
}
}
{
"user_id": 42,
"billing_country_code": "US"
}
8. Handle Large Numbers Carefully
JavaScript's JSON.parse() uses IEEE 754 double-precision floating point for all numbers, which can only safely represent integers up to 2^53 - 1 (9,007,199,254,740,991). IDs and timestamps larger than this lose precision when parsed in JavaScript.
The solution: for large integer IDs (such as Twitter's snowflake IDs), return both a numeric and a string representation: "id": 944985814402506752, "id_str": "944985814402506752". Consumers that need precision can use the string.
9. Use null for Optional Missing Fields
When a field is present in your schema but has no value for a particular resource, explicitly include it as null rather than omitting it. Omitting fields makes it harder to distinguish between "this field doesn't exist in the schema" and "this field exists but has no value". Consistent field presence makes API responses predictable.
10. Version Your API
As your API evolves, you'll need to make breaking changes to your JSON structure. Plan for this from day one by versioning your API, either via URL path (/api/v1/users), query parameter (?version=2), or Accept header (Accept: application/vnd.yourapi.v2+json).
Work with JSON Faster
Use our free JSON tools to minify, beautify, and validate JSON in seconds.