ConfigGenerator

How to Validate JSON Like a Pro

Learn the common causes of invalid JSON, how to fix them, and how to automate JSON validation in your workflows.

The Frustration of Invalid JSON

We've all been there: a single missing comma or a misplaced quote breaks the entire application. Because JSON is a strict format, any minor syntax error causes standard parsers (like JSON.parse()) to throw an exception.

Common JSON Errors

  1. Trailing Commas: Unlike JavaScript objects, JSON does not allow a comma after the last item in an array or object.
  2. Unquoted Keys: In JSON, all object keys must be enclosed in double quotes. { name: "John" } is invalid; { "name": "John" } is valid.
  3. Single Quotes: JSON only supports double quotes for strings. 'Hello' is invalid.
  4. Comments: Standard JSON does not support comments (though formats like JSONC do).

Validating JSON

The fastest way to validate JSON is using an online formatter or a linter in your IDE. A good validator won't just say "Unexpected token", it will pinpoint the exact line and column of the error.

Automating Validation

If you maintain JSON configuration files in a repository, you should automate validation to prevent broken builds:

  • Pre-commit Hooks: Use tools like Husky and prettier --check to validate JSON before it is committed.
  • CI Pipelines: Add a JSON linting step to your GitHub Actions or GitLab CI pipeline.
  • JSON Schema: For advanced validation, use JSON Schema to ensure the JSON structure contains the correct fields and data types, not just correct syntax.