X Xerobit

JSON Validator Online — Validate JSON Syntax and Structure

A JSON validator checks whether your JSON is syntactically correct and tells you exactly where errors are. Here's how validation works, the most common JSON syntax errors, and...

Mian Ali Khalid · · 5 min read
Use the tool
JSON Formatter
Format, validate, and beautify JSON online. 100% client-side — your data never leaves your browser.
Open JSON Formatter →

A JSON validator parses a JSON string according to the JSON specification (RFC 8259) and reports whether it’s valid. If it’s invalid, it reports the exact line and character where the error occurs. Valid JSON can be parsed without errors by any compliant JSON parser.

Use the JSON Formatter to validate and format JSON — it validates as part of the formatting process.

What JSON validation checks

JSON has strict syntax rules. Validation checks all of them:

  1. Valid structure: Starts with {, [, ", a number, true, false, or null
  2. Proper nesting: Every { has a matching }, every [ has a matching ]
  3. Quoted string keys: Object keys must be double-quoted strings
  4. No trailing commas: The last element in an object or array has no comma
  5. No comments: JSON has no comment syntax
  6. Valid escape sequences: Strings use valid escape sequences only
  7. Valid values: Only strings, numbers, objects, arrays, true, false, null

The 8 most common JSON errors

1. Trailing comma

// INVALID — trailing comma after last element:
{
  "name": "Alice",
  "age": 30,
}

// VALID:
{
  "name": "Alice",
  "age": 30
}

This is valid JavaScript (object literals allow trailing commas in modern JS) but invalid JSON. The most frequent JSON error from developers who write JSON like JavaScript.

2. Single quotes

// INVALID — single quotes:
{'name': 'Alice'}

// VALID — double quotes only:
{"name": "Alice"}

JSON requires double quotes for strings and keys. Single quotes are a JavaScript convenience, not part of JSON.

3. Unquoted keys

// INVALID:
{name: "Alice", age: 30}

// VALID:
{"name": "Alice", "age": 30}

JavaScript objects allow unquoted keys; JSON does not. All keys must be quoted strings.

4. Comments

// INVALID — JSON has no comments:
{
  // User data:
  "name": "Alice",
  /* Age field */
  "age": 30
}

// VALID — remove comments:
{
  "name": "Alice",
  "age": 30
}

If you need comments in your configuration files, consider YAML, JSONC, or JSON5 instead of JSON.

5. Undefined, NaN, Infinity

// INVALID — not valid JSON values:
{
  "value": undefined,
  "ratio": NaN,
  "max": Infinity
}

// VALID alternatives:
{
  "value": null,
  "ratio": null,
  "max": 1e308
}

These are JavaScript values, not JSON values. JSON’s undefined is just the absence of a key. JSON’s NaN and Infinity have no equivalents — use null or omit them.

6. Unescaped special characters in strings

// INVALID — unescaped special characters:
{"message": "He said "hello""}
{"path": "C:\Users\Alice"}

// VALID — escaped:
{"message": "He said \"hello\""}
{"path": "C:\\Users\\Alice"}

JSON strings must escape: \", \\, \/, \b, \f, \n, \r, \t, \uXXXX.

7. Numbers with leading zeros

// INVALID — leading zeros in numbers:
{"code": 007, "zip": 01234}

// VALID — no leading zeros:
{"code": 7, "zip": "01234"}

JSON number literals can’t have leading zeros. ZIP codes and phone numbers with leading zeros should be strings, not numbers anyway.

8. Incorrect nesting

// INVALID — mismatched brackets:
{"items": [1, 2, 3}

// VALID:
{"items": [1, 2, 3]}

Validating in code

JavaScript

function isValidJSON(str) {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
}

// Parse and catch errors:
try {
  const data = JSON.parse(jsonString);
  // Use data...
} catch (e) {
  console.error('JSON parse error:', e.message);
  // e.message includes position info:
  // "Unexpected token } in JSON at position 42"
}

Python

import json

def is_valid_json(s):
    try:
        json.loads(s)
        return True
    except json.JSONDecodeError:
        return False

# With error details:
try:
    data = json.loads(json_string)
except json.JSONDecodeError as e:
    print(f"JSON error at line {e.lineno}, col {e.colno}: {e.msg}")

JSON Schema validation

Beyond syntax, JSON Schema validates data structure — field types, required fields, value ranges:

from jsonschema import validate, ValidationError

schema = {
    "type": "object",
    "required": ["name", "age"],
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0, "maximum": 150}
    }
}

try:
    validate(instance={"name": "Alice", "age": 30}, schema=schema)
    print("Valid!")
except ValidationError as e:
    print(f"Schema validation error: {e.message}")

Command line

# Validate with jq (returns 0 if valid, non-zero if invalid):
jq . input.json > /dev/null 2>&1 && echo "Valid" || echo "Invalid"

# Python one-liner:
python3 -c "import json,sys; json.load(sys.stdin)" < input.json

JSONC and JSON5

If you need JSON with comments (common for configuration files), consider:

JSONC (JSON with Comments): Used by VS Code settings, supports // and /* */ comments. Not parseable by standard JSON.parse().

JSON5: Allows comments, trailing commas, unquoted keys, single quotes, and more. Has a separate parser library.

// JSONC/JSON5 (not valid JSON):
{
  // Database config:
  "host": "localhost",
  "port": 5432,
  "name": "mydb",  // trailing comma OK
}

For configuration files you control, JSONC or JSON5 is more developer-friendly. For API communication, use strict JSON.


Related posts

Related tool

JSON Formatter

Format, validate, and beautify JSON online. 100% client-side — your data never leaves your browser.

Written by Mian Ali Khalid. Part of the Data & Format pillar.