Developer tools

How to validate JSON and understand its most common errors

JSON syntax rules according to the standard, the five mistakes behind almost every parse failure, and how to validate a file in the browser, terminal or Node.js.

Validating JSON means checking that the text follows the grammar defined in RFC 8259: objects in braces with double-quoted keys, arrays in brackets, values separated by commas with no trailing comma, and no comments. Almost every parse error comes from five causes: trailing commas, single quotes, comments, unquoted keys and disallowed values such as undefined or NaN. You can validate it in seconds with an online tool, with jq in the terminal or with JSON.parse in Node.js.

This article reviews the rules, shows how to read the error messages and compares the ways to validate depending on your context.

The JSON rules in six lines

  1. A JSON document is a value: object, array, string, number, true, false or null.
  2. Objects go between {} and contain "key": value pairs separated by commas.
  3. Arrays go between [] and contain values separated by commas.
  4. Strings always use double quotes; inside, special characters are escaped with \ (\", \\, \n, \uXXXX).
  5. Numbers have no quotes, no leading zeros, and no forms like .5 or +1; 1e3 is valid.
  6. There are no comments, trailing commas, undefined, NaN, Infinity or dates as a native type (they are written as strings).

A valid example:

{
  "name": "AIMRAN Tools",
  "version": 2,
  "active": true,
  "tags": ["images", "json", "text"],
  "author": { "name": "Aimran", "web": "https://aimran.es" },
  "publishedAt": "2026-10-06"
}

The five most common errors

1. Trailing comma

{ "a": 1, "b": 2, }

Valid in JavaScript, invalid in JSON. Node.js 22 rejects it with Expected double-quoted property name in JSON at position 18 (line 1 column 19): after the comma it expects another key and finds }.

2. Single quotes

{ 'a': 1 }

JSON only allows double quotes. Node.js 22 returns Expected property name or '}' in JSON at position 2 (line 1 column 3): the parser reaches the single quote and does not recognise it as the start of a key. This is the typical error when copying an object from Python or JavaScript code.

3. Comments

{
  // configuration
  "debug": true
}

Neither // nor /* */ exist in JSON. Some derived formats (JSONC, JSON5) allow them, which is why files such as tsconfig.json or VS Code’s settings.json tolerate them, but a strict parser rejects them.

4. Unquoted keys

{ name: "Aimran" }

In JavaScript it is a valid object literal; in JSON the key must be double-quoted. Node.js returns the same message as with single quotes (Expected property name or '}'), because in both cases what is missing is the double quote.

5. Values that do not exist in JSON

{ "total": NaN, "date": undefined }

NaN, Infinity and undefined are not JSON values. When serialising with JSON.stringify in JavaScript, undefined properties disappear and NaN becomes null, which sometimes hides the problem until another system reads the file.

Less frequent errors: typographic quotes (“ and ”) pasted from a word processor, a BOM character at the start of the file, or a number with a leading zero (007).

How to read an error message

Parsers report the position (character index) or the line and column where they stopped understanding the text. That point is where the parser gave up, which is not always where the real error is: an unclosed brace on line 3 may be reported at the end of the file. If the message does not take you straight to the problem, look backwards from the reported position for the last element that was still valid.

Python 3.13, for example, is more descriptive with the trailing comma: Illegal trailing comma before end of object: line 1 column 17 (char 16). Earlier versions gave the generic message Expecting property name enclosed in double quotes.

Ways to validate JSON

Context Tool Command or usage
Quick, nothing to install Online validator Paste the text into the JSON formatter on AIMRAN Tools: it highlights the error line and formats the result without sending the data to any server.
Terminal (Linux, macOS, WSL) jq jq . file.json prints the formatted JSON or the error with its line. jq empty file.json only validates.
Terminal, nothing to install Python python3 -m json.tool file.json
JavaScript project Node.js node -e "JSON.parse(require('fs').readFileSync('file.json','utf8'))"
Editor VS Code, Zed, etc. They flag syntax errors as you type; make sure the file is in JSON mode and not JSONC.
Validating structure, not just syntax JSON Schema Define which keys and types are expected and validate with a library such as Ajv (JavaScript) or jsonschema (Python).

For sensitive data (configurations with keys, user exports), prefer tools that work locally or in the browser without uploading the content.

Validating syntax is not validating content

A syntactically correct JSON document can still be useless for your application: a misspelled key, a number where you expected a string, a missing required field. That is what JSON Schema solves, a standard for describing the expected shape of a document. If you consume third-party JSON or expose an API, it is worth defining a schema and validating it at the boundary of your system.

Conclusion

JSON is a small format with strict rules, and most errors come from treating it as if it were JavaScript: trailing commas, single quotes, comments and unquoted keys. To find the fault, use a validator that reports the position (online, jq, json.tool or JSON.parse) and remember that the reported point is where the parser stopped, not necessarily where the problem started. And if the JSON is part of a contract between systems, add JSON Schema to validate the content too.

Sources and references

  1. RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format rfc-editor.org
  2. json.org: Introducing JSON json.org
  3. MDN: JSON.parse() developer.mozilla.org
  4. jq: official manual jqlang.github.io
  5. Python: json module (json.tool) docs.python.org

Related tools

Herramientas gratuitas de AIMRAN Tools que funcionan en tu navegador, sin registro.

Ver todas las herramientas