2026-05-0612 min read

How to Format and Validate JSON — Fix Errors Fast

Learn to format, validate, and debug JSON. Common syntax errors explained with fixes, plus how to validate JSON in JavaScript, Python, and curl.

JSON is everywhere in web apps — API responses, config files, and logs. A single trailing comma or unquoted key can break parsing, so formatting and validation save real debugging time.

The 8 most common JSON syntax errors and how to fix them

JSON is strict. One wrong character breaks the entire document. Here are the errors developers hit most often:

1. Trailing comma Wrong: {"name": "Alice", "age": 30,} Right: {"name": "Alice", "age": 30} JSON does not allow a comma after the last item in an object or array.

2. Single quotes instead of double quotes Wrong: {'name': 'Alice'} Right: {"name": "Alice"} JSON requires double quotes for all strings and keys. Single quotes are JavaScript syntax, not JSON.

3. Unquoted keys Wrong: {name: "Alice"} Right: {"name": "Alice"} Every key in a JSON object must be wrapped in double quotes.

4. Comments Wrong: {"name": "Alice" // this is the user} Right: {"name": "Alice"} JSON does not support comments of any kind (neither // nor /* */).

5. Undefined or NaN values Wrong: {"score": undefined, "result": NaN} Right: {"score": null, "result": null} JSON supports null but not JavaScript's undefined or NaN.

6. Trailing decimal point Wrong: {"price": 9.} Right: {"price": 9.0} Numbers must have digits on both sides of the decimal point.

7. Missing comma between properties Wrong: {"name": "Alice" "age": 30} Right: {"name": "Alice", "age": 30} Properties must be separated by commas.

8. Mismatched brackets Wrong: {"items": [1, 2, 3} Right: {"items": [1, 2, 3]} Opening [ must close with ] and opening { must close with }.

Validating JSON in code

JavaScript (browser and Node.js):
try {
  const data = JSON.parse(jsonString);
  console.log('Valid JSON', data);
} catch (e) {
  console.error('Invalid JSON:', e.message);
}
Python:
import json
try:
    data = json.loads(json_string)
    print('Valid JSON')
except json.JSONDecodeError as e:
    print(f'Invalid JSON: {e}')
curl (check an API response):
curl -s https://api.example.com/data | python3 -m json.tool
# Outputs formatted JSON if valid, error message if invalid
Node.js (validate a file):
const fs = require('fs');
try {
  JSON.parse(fs.readFileSync('data.json', 'utf8'));
  console.log('Valid');
} catch(e) {
  console.error('Invalid:', e.message);
}

JSON schema validation — beyond syntax

Syntax validation only checks that JSON is parseable. Schema validation checks that the structure and data types match what your application expects.

JSON Schema is the standard for this. Example schema for a user object:

{
  "type": "object",
  "required": ["id", "email"],
  "properties": {
    "id": {"type": "integer"},
    "email": {"type": "string", "format": "email"},
    "age": {"type": "integer", "minimum": 0, "maximum": 150}
  }
}

Libraries: ajv (JavaScript/Node.js), jsonschema (Python), Newtonsoft.Json (C#). Schema validation catches data type errors, missing required fields, and out-of-range values that syntax validation misses entirely.

Try it instantly

Format and validate JSON in the browser with our JSON Formatter & Validator:

FAQ

Why is my JSON valid in JavaScript but failing in an API? JavaScript is more permissive than strict JSON. JS allows trailing commas, single quotes, and comments — JSON does not. Always validate with a strict JSON parser, not a JavaScript eval or console.

What is the difference between JSON and JSONP? JSON is a data format. JSONP (JSON with Padding) was a workaround for cross-origin requests before CORS existed. It wraps JSON in a function call: callback({"data": "value"}). JSONP is largely obsolete — use CORS headers instead.

Can JSON contain binary data like images? Not directly. Binary data must be base64 encoded first, then stored as a string. This is why image APIs often return base64 strings or URLs rather than raw binary in JSON responses.

What is the maximum size for a JSON file? JSON has no built-in size limit. Practical limits come from the parser. Most browsers handle JSON up to a few hundred MB. For very large datasets (gigabytes), use streaming parsers (like JSONStream in Node.js) rather than loading everything into memory at once.

Should I use JSON or XML? JSON for almost everything modern. JSON is lighter, easier to read, and natively supported in JavaScript. XML is still used in legacy enterprise systems, RSS/Atom feeds, SOAP APIs, and document formats like docx and xlsx (which are ZIP files containing XML internally).

Try it yourself

JSON Formatter & Validator

Pretty-print JSON and catch syntax errors before you ship code.

Explore more tools in the Tools Directory.
Browse all articles →