DeveloperTools
JSON Tools

How to Validate JSON: Common Errors and Solutions

Learn how to validate JSON data, identify common syntax errors, and fix them. Includes examples of invalid JSON and solutions.

JSON validation is the process of checking whether a JSON document conforms to the JSON specification (RFC 8259). A valid JSON document must follow strict syntax rules, and even a single character error can cause parsing to fail entirely.

JSON Syntax Rules

JSON supports six data types: objects, arrays, strings (double-quoted), numbers, booleans (true/false), and null. Every JSON document must contain exactly one root value.

The 10 Most Common JSON Errors

  1. Trailing commas — A comma after the last element. Valid in JavaScript but not JSON.
  2. Single-quoted strings — JSON requires double quotes for all strings.
  3. Unquoted keys — Object keys must be wrapped in double quotes.
  4. Missing quotes around values — String values must be quoted.
  5. JavaScript expressions — JSON doesn't support functions, dates, or undefined.
  6. Control characters — Newlines must be escaped as \n, tabs as \t.
  7. Duplicate keys — Causes unpredictable behavior in parsers.
  8. Empty input — An empty string is not valid JSON.
  9. Extra data — Content after the closing bracket makes the document invalid.
  10. Invalid escapes — Only specific escape sequences are allowed.

How to Debug JSON Errors

When validation fails, the error message usually includes the position of the first error. Start checking from that position. Format the JSON first to make the structure visible, then check for invisible characters like BOM.

Frequently Asked Questions

What is a JSON schema?
A JSON Schema validates the structure and data types of a JSON document. It defines required fields, value types, patterns, and constraints beyond basic syntax validation.
How do I validate JSON in JavaScript?
Use JSON.parse() inside a try-catch block. If parsing succeeds, the JSON is valid. For structural validation, use a JSON Schema validator like Ajv.
Can JSON contain special characters?
Yes, but they must be escaped: quotes as \", backslashes as \\, newlines as \n, and Unicode characters using \uXXXX notation.