Invalid JSON is one of the most common causes of API bugs. A missing quote, a trailing comma, or an unescaped character can break everything. This guide shows you how to quickly find and fix JSON errors using tools and code.

What makes JSON invalid?

JSON has strict syntax rules. The most common violations are:

1. Trailing commas

// Invalid
{
  "name": "Alice",
  "age": 28,   // trailing comma - not allowed
}

// Valid
{
  "name": "Alice",
  "age": 28
}

2. Single quotes instead of double quotes

// Invalid - JSON requires double quotes
{ 'name': 'Alice' }

// Valid
{ "name": "Alice" }

3. Unquoted keys

// Invalid
{ name: "Alice" }

// Valid
{ "name": "Alice" }

4. Comments

// Invalid - JSON does not support comments
{
  // This is a user object
  "name": "Alice"
}

// Valid - remove all comments

5. Undefined or function values

// Invalid - these are JavaScript, not JSON
{ "fn": function() {} }
{ "val": undefined }

// Valid alternatives
{ "fn": null }
{ "val": null }

6. Unescaped special characters in strings

// Invalid - quotes inside strings must be escaped
{ "message": "He said "hello"" }

// Valid
{ "message": "He said \"hello\"" }

// Also valid
{ "path": "C:\\Users\\Alice" }  // backslashes must be doubled

Method 1: Online JSON validator (fastest)

Try it free — JSON Formatter & Validator

Paste your JSON — invalid syntax is highlighted with the exact error message. Free, instant, no upload.

Open tool →

Method 2: Validate JSON in JavaScript

function validateJSON(str) {
  try {
    JSON.parse(str);
    return { valid: true };
  } catch (error) {
    return {
      valid: false,
      error: error.message
    };
  }
}

const result = validateJSON('{"name": "Alice",}');
console.log(result);
// { valid: false, error: "Unexpected token }" }

Method 3: Validate JSON in Python

import json

def validate_json(json_string):
    try:
        json.loads(json_string)
        return True, None
    except json.JSONDecodeError as e:
        return False, str(e)

valid, error = validate_json('{"name": "Alice",}')
print(valid)  # False
print(error)  # Trailing comma causes error at line 1 col 19

Method 4: Validate JSON schema

Basic JSON validation checks syntax. Schema validation checks that the structure and types are correct:

// Using ajv (JavaScript)
const Ajv = require('ajv');
const ajv = new Ajv();

const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age:  { type: 'number', minimum: 0 }
  },
  required: ['name', 'age']
};

const validate = ajv.compile(schema);
const valid = validate({ name: 'Alice', age: 28 });
console.log(valid); // true

const invalid = validate({ name: 'Alice' }); // missing age
console.log(validate.errors);
// [{ message: "must have required property 'age'" }]

Reading JSON error messages

JSON parse errors always include a position. Learn to read them:

  • Unexpected token } at position 42 — likely a trailing comma before the }
  • Unexpected token ' at position 1 — single quotes used instead of double
  • Unexpected end of JSON input — string is cut off, missing closing bracket
  • Unexpected token u in JSON at position 0 — the value is undefined (JavaScript)