JSON Editing Best Practices
Small mistakes in JSON are easy to make and expensive to debug. These best practices cover formatting, validation, naming, nesting, and tooling — so your JSON stays clean and valid at every step.
JSON editing best practices are a set of habits and rules that prevent structural errors, maintain consistency across a codebase, and make JSON easier to read, diff, and debug — whether you are editing manually, generating programmatically, or reviewing in a code review.
1. Always Validate Before Using
Invalid JSON causes runtime errors that can be difficult to trace. A missing comma, an extra bracket, or a trailing comma after the last field will silently break a parser. Validate every time you finish editing — before pasting into an API client, before committing to a repository, and before deploying to production.
Validation takes seconds. Debugging a production error caused by a single malformed character can take hours. Make validation a habit, not an afterthought.
2. Use Consistent Formatting
Pick an indentation style — 2 spaces or 4 spaces — and apply it consistently across all JSON files in a project. Mixing styles makes diffs noisy and harder to review. Most teams use 2-space indentation for JSON because it keeps nested structures compact without sacrificing readability.
Consistent 2-space indentation
{
"user": {
"name": "Alice",
"role": "admin",
"teams": ["platform", "security"]
}
}For JSON that is stored in environment variables, configuration management systems, or transmitted over the wire, use minified (no-whitespace) JSON instead. Keep the pretty-printed version as the source of truth in version control and minify it as part of your build or deployment process.
3. Follow Key Naming Conventions
JSON does not enforce a naming convention for keys, but consistency matters. The two most common conventions are camelCase (used in JavaScript APIs and most REST responses) and snake_case (common in Python APIs and databases). Pick one and apply it everywhere in a given API or file.
camelCase (JS/REST)
{
"firstName": "Alice",
"lastName": "Chen",
"createdAt": "2026-01-01"
}snake_case (Python/DB)
{
"first_name": "Alice",
"last_name": "Chen",
"created_at": "2026-01-01"
}Avoid mixing conventions within the same object. If you receive snake_case from a database and need to expose camelCase via an API, transform at the boundary — do not mix styles inside a single response object.
Keep key names descriptive but concise. Abbreviations like usr save a few bytes but hurt readability. Prefer user. JSON is typically minified anyway, so key length rarely matters for performance.
4. Avoid Unnecessary Deep Nesting
Deeply nested JSON is hard to read, hard to edit, and hard to query. If you find yourself writing code like data.user.profile.address.city, the structure may be over-engineered.
✗ Over-nested
{
"data": {
"user": {
"profile": {
"address": {
"city": "London"
}
}
}
}
}✓ Flatter
{
"user": {
"city": "London"
}
}A practical rule: if you need more than 3–4 levels of nesting, reconsider the structure. Flatter JSON is easier to serialize, deserialize, validate, and diff. When deep nesting is necessary (e.g., tree data), make sure it is intentional and well-documented.
5. Use Correct Value Types
JSON has six value types: string, number, boolean, null, object, and array. Use the type that matches the semantic meaning of the field — not just what passes validation.
✗ Avoid
"active": "true"✓ Prefer
"active": trueBooleans should not be strings. "true" !== true in most parsers.
✗ Avoid
"count": "42"✓ Prefer
"count": 42Numeric values should be numbers, not quoted strings, unless the value can exceed Number.MAX_SAFE_INTEGER.
✗ Avoid
"tags": "admin,editor"✓ Prefer
"tags": ["admin", "editor"]Use an array instead of a comma-delimited string when representing multiple values.
✗ Avoid
"created": 1714176000✓ Prefer
"created": "2024-04-27T00:00:00Z"ISO 8601 strings are more readable and timezone-safe than Unix timestamps for human-facing data.
6. Do Not Add Comments
Standard JSON does not support comments. Adding // ... or /* ... */ inside a JSON file will cause a parse error in any standard JSON parser.
If you need to annotate a JSON configuration file, consider these alternatives:
- tips_and_updates
Use JSON5 or JSONC
These supersets of JSON support comments and are supported by some tools (VS Code settings, TypeScript tsconfig).
- tips_and_updates
Add a "_comment" key
A convention used in some config files: { "_comment": "This field is deprecated", "legacyMode": false }. Not semantically ideal but widely understood.
- tips_and_updates
Document externally
Keep a README or schema (JSON Schema) alongside the file that explains each field.
7. Be Explicit About null vs Missing Keys
There is a semantic difference between a key set to null and a key that is absent entirely. Decide on a convention and apply it consistently.
Key present, value null — field exists but has no value
{ "middleName": null }Key absent — field not applicable or unknown
{ "firstName": "Alice" }For PATCH requests in REST APIs, the distinction matters even more: a missing key typically means “do not change this field”, while null may mean “clear this field”. Document your convention in an API spec or schema.
8. Handle Large JSON Carefully
Editing large JSON files (>1 MB) by hand is error-prone. A few guidelines:
- checkUse a tool with folding/collapsing so you can navigate to the section you need without scrolling through thousands of lines.
- checkEdit only the specific section you need — copy out the relevant subtree, edit it in isolation, then replace it back.
- checkAfter editing, diff the result against the original to confirm only the intended changes were made.
- checkFor programmatic edits on very large JSON, use a script (jq, Python json module) rather than manual editing to avoid introducing whitespace or structural errors.
9. Keep JSON in Version Control
Configuration JSON, seed data, and API schemas belong in version control alongside your code. This gives you a history of every change, a way to review edits before they are applied, and the ability to roll back a bad edit instantly.
Use consistent formatting (same indentation, sorted keys if your team agrees) so that diffs show only meaningful changes. A reformat of an existing JSON file creates a noisy diff that is hard to review — format once, consistently, and never reformat without a deliberate reason.
Do not commit secrets — API keys, database passwords, private tokens — in JSON files. Use environment variables or a secrets manager and reference them in your code. If a secret is accidentally committed, rotate it immediately.
10. Use the Right Tools
Manual JSON editing without tooling support is the root cause of most JSON errors. The right tools catch mistakes as you make them.
JSON Editor
Syntax highlighting, real-time validation, tree view — catches errors as you type.
JSON Validator
Confirm JSON is valid before deploying or sharing.
JSON Formatter
Apply consistent indentation in one click.
JSON Minify
Strip whitespace for environment variables and wire transmission.
JSON Compare
Diff two versions of a JSON file to confirm only intended changes were made.
JSON Sorter
Sort keys alphabetically for consistent, diffable JSON.
Frequently Asked Questions
What is the most common JSON editing mistake?expand_more
The trailing comma — adding a comma after the last item in an object or array. Standard JSON does not allow trailing commas. The second most common mistake is using single quotes instead of double quotes for strings or keys.
Should JSON keys be camelCase or snake_case?expand_more
Either is valid in JSON. The choice depends on context: camelCase is conventional for JavaScript and REST APIs, snake_case for Python and database-facing data. What matters most is consistency within a project.
Can I add comments to JSON?expand_more
No — standard JSON does not support comments. Parsers will throw an error if they encounter // or /* */. Use JSON5 or JSONC if you need comments, or document the fields in an external schema or README.
What is the difference between null and a missing key in JSON?expand_more
A null value means the key exists but has no value. A missing key means the field was not included at all. In REST APIs, the distinction is critical for PATCH requests: missing typically means "do not change", null often means "clear the value".
How do I safely edit a large JSON file?expand_more
Use an editor with tree view and folding so you can navigate to the specific section. Edit the relevant subtree in isolation, then replace it. After editing, diff the result against the original to verify only the intended changes were made.
Should I format JSON with 2 spaces or 4 spaces?expand_more
2 spaces is the most common convention and keeps nested structures compact. 4 spaces gives slightly more visual separation. Either is fine — consistency within a project matters more than which you choose.