JSON looks simple until a parser rejects it. A missing comma, a single quote or a trailing comma can stop an API response from loading. The good news is that most JSON errors are easy to spot once you know what the parser is complaining about.
What validation means
JSON validation checks that the text follows the JSON grammar. It does not check that the data makes sense for your application. A document can be valid JSON and still be missing a required field, use the wrong type or contain an impossible date.
There are two useful steps: syntax validation, then schema validation. Start with syntax. There is no point checking field names until the parser can read the file at all.
Use a local validator
Paste the JSON into the NelcByte JSON formatter. It uses the browser's own parser, so the input stays on your device. If you are checking production data, avoid random online validators that may log or store what you paste.
Common JSON errors
| Problem | Invalid example | Fix |
|---|---|---|
| Trailing comma | {"a": 1,} | Remove the comma after the last item |
| Single quotes | {'a': 1} | Use double quotes for keys and strings |
| Unquoted key | {a: 1} | Write {"a": 1} |
| Comment | // note | Remove the comment or use JSON5/JSONC |
| Missing comma | {"a": 1 "b": 2} | Add a comma between members |
| Smart quotes | “a”: 1 | Replace with straight double quotes |
| NaN or Infinity | {"value": NaN} | Use a number, string or null |
Duplicate keys are another trap. The JSON specification says names should be unique. Many parsers keep the last value, but relying on that behaviour is a bad idea. Pick one key and remove the duplicate.
Read the error message
A parser message usually includes a position or a line and column. For example:
Unexpected token } in JSON at position 42
The position is a character offset from the start of the text. Count from there, or use a formatter that calculates the line for you. Look immediately before the reported token: a missing comma often shows up at the next key, not at the place where the comma is missing.
Different browsers word the same problem differently. If one message is unhelpful, run the same input through a second parser or a command-line tool.
Validate structure with JSON Schema
Syntax validation is only the first step. If your application expects a name string and a price number, a valid JSON document can still break it.
JSON Schema is a vocabulary for describing the shape of JSON. You can require fields, set types, limit string lengths and reject unknown properties. Validators are available for most languages, and many API tools support schema files directly.
For a small internal script, a few explicit checks may be enough. For a public API or a data pipeline, a schema pays for itself quickly.
Validate in code
JavaScript:
const data = JSON.parse(text);
Python:
import json
with open("data.json", encoding="utf-8") as handle:
data = json.load(handle)
Command line with jq:
jq . data.json
Always handle the error path. A failed parse should produce a clear log message, not a blank screen or a stack trace showing the full payload.
Security and privacy
- Do not paste secrets, access tokens or personal data into a site you do not trust.
- Use a local validator for production data.
- Do not use
eval()to parse JSON. Use a real parser. - Limit the size of JSON you accept from users so one request cannot exhaust memory.
- Treat parsed values as untrusted input and validate them before use.
A short checklist
- Format the JSON to see the structure.
- Fix syntax errors from the reported position.
- Check duplicate keys and unexpected types.
- Validate the shape against a schema if you have one.
- Test the error path in your application.
Format, validate and minify JSON in your browser. Errors include a line and column when the parser provides one.