How to format and validate JSON safely
Find JSON syntax errors, preserve data types and keep structured data readable while moving it between systems.
Pretty printing makes JSON easier to read, but readability is only the first check. A file can be valid JSON and still contain the wrong fields, strings where numbers were expected, or private data that should not be shared.
Key takeaways
- Format before debugging syntax.
- Validation confirms grammar, not business meaning.
- Check data types and field names after conversion.
Format a copy first
Indentation reveals the real nesting structure and makes missing commas, braces and quotes easier to locate. Keep the original payload until the problem is understood.
Start with the first parser error
Later errors are often a consequence of the first broken character. Fix the earliest reported position, then validate again.
Watch strings and numbers
The value 12 is different from the string "12", and true is different from "true". A formatter should preserve those types exactly.
Use minification for transport
Readable JSON belongs in source control and debugging tools. Minified JSON can reduce transfer size, but it is harder for a person to review.
Validate the schema separately
Syntax validation cannot tell whether required fields are missing or whether a date follows your application’s rules. Use a schema or application-level validation for that.
Final check
When JSON crosses a system boundary, check three layers: valid syntax, expected structure and appropriate content.