How to validate JSON: syntax, JSON Schema and duplicate keys
Learn the difference between JSON syntax and schema validation, how to read an error's line and column, and when JSON Schema is worth it.
JSON can be perfectly valid and still break your app. It sails through the parser, yet price arrives as a string, email is missing, or the list of items is empty. The opposite happens too: a single misplaced character makes a system reject the whole file.
The difference lies in the question you ask the file. One is about syntax: “is this JSON at all?” The other is about structure: “is this data usable by my system?” Keep the JSON validator open in another tab to try the examples.
Syntax validation vs. schema validation
Syntax validation answers one question: is this text well-formed JSON? It checks quotes, commas, braces and value types. Schema validation comes next and asks: does this data have the shape my system expects? Required fields, types, ranges and allowed values.
Order matters. If the syntax fails, there's no object to compare against anything — the parser stops at the first error. When debugging, always start with syntax.
What makes JSON valid?
JSON is valid when it follows the grammar in RFC 8259: a single root value, strings and keys in double quotes, numbers without leading zeros, only true, false and null as literals, and no comments or trailing commas. Any deviation, however small, invalidates the entire document.
A few details that catch people coming from JavaScript or Python:
- The root can be any value. Since RFC 7159, a bare
"ok",42ornullis valid JSON too — not just objects and arrays. NaNandInfinitydon't exist in JSON. Some libraries emit them, but they fail a strict validator.- The spec sets no number limit, but parsers do. In JavaScript, integers above 9,007,199,254,740,991 can lose precision. Long IDs should travel as strings.
- Text must be UTF-8 when exchanged between systems that aren't part of the same closed ecosystem, and control characters inside strings need escaping (
\n,\t).
The everyday rule-breakers — single quotes, trailing commas, comments — are covered in detail in how to format JSON and fix common errors.
How to read the line and column of an error
The line and column point to the character where the parser noticed the problem. The mistake is almost always right there or just before it. Here's how the validator shows a trailing comma after the last field of an object:
3 | "price": 12.50,
^The caret points at the comma itself, the character that breaks the rule. A browser's JSON.parse, on the other hand, usually reports the same error at the } on the next line, because only then does it realize no other property is coming. That's why positions differ between tools: each one reports where it stopped reading.
With a missing closing quote, the error shows up at the end of the same line where the string was opened, since JSON doesn't allow a raw line break inside a string. In minified JSON, where everything sits on one line, the column can land much further along; in that case, look for the last quote opened before the caret.
Validating API responses and config files
In practice, validation comes up in three places:
| Situation | What to validate | Tip |
|---|---|---|
| An API call that failed | Syntax first | An HTML error page (<!DOCTYPE) instead of JSON is the usual cause of “unexpected token <”. |
| Config files | Syntax and schema | Editors such as VS Code already check package.json and tsconfig.json against published schemas and underline fields with the wrong name or type. |
| Third-party data | Schema | Reject anything with the wrong shape early, before it hits your database. |
What is JSON Schema and when should you use it?
JSON Schema is a vocabulary, itself written in JSON, for describing the expected structure of a document: types, required fields, formats and limits. Use it when several parts of a system exchange the same data, when you accept JSON from outside sources, or when you want to document and automatically test an API contract.
A minimal schema for a product:
{
"type": "object",
"required": ["name", "price"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"price": { "type": "number", "minimum": 0 }
}
}With it, {"name": "Mug", "price": "$12.50"} has flawless syntax but fails the schema: the price came in as a string with a currency sign — exactly what happens when data is copied out of a formatted spreadsheet. The Util Hub validator only handles the first step, syntax; it doesn't apply schemas. For the second step, use a library such as Ajv (JavaScript) or jsonschema (Python) in your code or your automated tests.
For a small file or a one-off check, syntax validation is usually enough. A schema pays off when the same format repeats many times.
The duplicate key trap
RFC 8259 says names inside an object should be unique — a recommendation, not a ban. So {"status": "active", "status": "canceled"} gets through most parsers, and each language decides which value wins. In JavaScript and Python, the last one does: an “active” customer silently becomes “canceled”.
Duplicates tend to appear in hand-edited JSON or files stitched together by scripts. That's why the validator treats them as a warning: the document is valid, but every repeated key is listed with its line and column so you can decide.
Validate your JSON now
In the JSON validator, paste your content or import a .json file. It checks as you type, with the line, column and a plain-English explanation of any error, warnings for duplicate keys and a summary of the structure: root type, item count and depth. If your next step is getting the data into a spreadsheet, see how to convert JSON to CSV.
Frequently asked questions
Can valid JSON still cause errors in my app?
Yes. Syntax validation only guarantees the text is well-formed. If a required field is missing, has the wrong type or holds an unexpected value, your app can still fail. To catch those cases, check the data against a JSON Schema or add explicit checks in your own code before using it.
Why do I get “Unexpected token <” when reading an API response?
Almost always the server returned an HTML page — an error, login or maintenance page — instead of JSON. Its first character, <, can't start a JSON document. Check the HTTP status code and the response's Content-Type header before digging into the body itself.
How do I validate JSON on the command line or in CI?
With jq, jq empty file.json prints nothing when the JSON is valid and exits with an error code when it isn't. Without jq, python3 -m json.tool file.json > /dev/null does the same. In a CI pipeline, that non-zero exit code is enough to stop the build.
Do I need to learn JSON Schema to validate my files?
Not for one-off checks: a syntax validator solves most everyday problems. JSON Schema is worth it when you exchange the same data between systems regularly, accept JSON from third parties, or want automated tests that guarantee your API contract every time something changes.
Is JSON Lines (NDJSON) valid JSON?
Each line on its own is valid JSON, but the file as a whole isn't: it has several root values, one per line, and a strict validator rejects it from the second line on. Validate it line by line, for example with python3 -m json.tool --json-lines, or wrap the records in an array.