JSON Formatting and Validation Guide
A practical workflow for correcting JSON without damaging data, then reviewing, sharing, and deploying it with confidence. Learn the difference between syntax and schema validation, the semantic defects valid JSON can still contain, and how to handle large or sensitive payloads.
Written and reviewed by yuuLast reviewed:
1. Separate formatting, syntax, and schema validation
Formatting adds indentation and line breaks for review. If it succeeds, braces, quotes, and separators are parseable JSON, but that says nothing about whether an API receives the fields and types it requires.
For example, an age value of "20" is valid JSON but still wrong when an API requires an integer. A reliable workflow parses and formats first, then validates required fields, types, formats, ranges, and allowed values with JSON Schema.
- Formatting: normalize indentation and key presentation for review.
- Syntax validation: find invalid commas, quotes, brackets, and escapes.
- Schema validation: enforce required fields, types, formats, enums, and extra-property rules.
2. Repair syntax errors efficiently
Start at the reported line and column and inspect only the surrounding lines. The character before that location—often a comma, closing bracket, or string boundary—is frequently the real cause. If a browser cannot report a position, reduce the payload into smaller arrays or objects and test each part.
Change one location, parse again, and proceed to the next reported issue. One early delimiter mistake can create many later errors, so bulk editing usually makes diagnosis harder.
- Trailing commas: remove the comma after the final array item or property.
- Quotes: keys and strings require double quotes; escape quotes inside string values.
- Comments: // and /* */ belong to JSONC, not standard JSON.
- Unsupported values: undefined, NaN, Infinity, and functions are not JSON values.
3. Find defects that valid JSON hides
Duplicate keys are syntactically accepted by many parsers, but later values commonly overwrite earlier ones. Mixed ID types, userId versus userID, timezone-free dates, and confusion between null and a missing field are other frequent integration failures.
Sorting object keys can make code review easier, but do not reorder arrays. Array order may encode priority, display position, or processing order; presentation cleanup and semantic changes must remain separate.
- Search for duplicated key names.
- Confirm IDs, money, and booleans use the expected types.
- Require a timezone or UTC offset for timestamps that represent an instant.
- Decide whether null, empty string, empty array, and absence mean different things.
4. Review large and sensitive payloads
Formatting expands a compact payload and can increase browser memory and rendering time. Extract the smallest record set that reproduces the problem before pasting several megabytes.
Before sharing logs or responses, remove access tokens, cookies, email addresses, and internal URLs. Replacing values with representative dummy data preserves the structure needed for debugging while reducing exposure.
5. Use a deployment-ready workflow
Review a diff before overwriting the source file. Confirm that key sorting and indentation changes do not hide value changes, then run schema validation, load the result in the target application, and commit the reviewed file.
Configuration JSON also needs environment review. A valid production file can still fail because it contains a development endpoint, an incorrect region, or the wrong permission value.
Pre-review checklist
- Zero syntax errors
- Required fields and types validated
- No duplicate keys
- Timestamp timezone checked
- Production secrets and PII removed
- Array order unchanged unless intended
- Formatted diff reviewed
- Loaded in the target application
Related tools
Primary references
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange FormatThe standard definition of JSON grammar, values, encoding, and interoperability considerations.
- JSON Schema Draft 2020-12The JSON Schema specification targeted by the site's schema validator.