·8 min read·JSON & Data

How to Validate JSON: A Complete Guide for Developers

Learn how to validate JSON data, fix common syntax errors, and understand why JSON validation matters for APIs, configs, and data exchange. Includes practical examples and free tools.

Try our free JSON Formatter & Validator

Paste JSON, get instant validation with error highlighting and auto-fix.

Open Tool

What Is JSON and Why Does It Matter?

JSON (JavaScript Object Notation) is the most widely used data interchange format on the web. Created by Douglas Crockford in the early 2000s, it has become the standard for API communication, configuration files, data storage, and message passing between services. According to the 2025 Stack Overflow Developer Survey, over 90% of web developers work with JSON daily.

JSON's popularity stems from its simplicity — it uses a small set of rules that are easy to read and write for both humans and machines. But that simplicity is also its weakness: a single misplaced comma, a missing quote, or an unescaped character can break an entire application. This is why JSON validation is a critical skill for every developer.

Invalid JSON causes silent failures in APIs, configuration errors in deployments, data corruption in databases, and hours of debugging frustration. Learning to validate and fix JSON properly will save you significant time and prevent production incidents.

What Makes JSON Valid?

Valid JSON must follow the rules defined in RFC 8259. Here are the key rules:

  • 1. Strings must use double quotes — single quotes ('hello') and backticks (`hello`) are not valid JSON.
  • 2. Keys must be quoted{name: "John"} is invalid. Use {"name": "John"}.
  • 3. No trailing commas{"a": 1, "b": 2,} is invalid because of the comma after 2.
  • 4. No comments — JSON does not support // comments or /* block comments */.
  • 5. Supported data types — strings, numbers, booleans (true/false), null, arrays, and objects. No undefined, NaN, Infinity, or functions.
  • 6. Numbers cannot have leading zeros007 is invalid. Use 7.

10 Most Common JSON Errors (and How to Fix Them)

Here are the errors that cause the most headaches for developers, ranked by how frequently they occur:

1. Trailing Comma

Invalid
{"name": "John", "age": 30,}
Valid
{"name": "John", "age": 30}

Remove the comma after the last property. This is the #1 JSON error because JavaScript objects allow trailing commas but JSON does not.

2. Single Quotes Instead of Double Quotes

Invalid
{'name': 'John'}
Valid
{"name": "John"}

JSON requires double quotes for all strings and keys. Single quotes are a JavaScript convention, not valid JSON.

3. Unquoted Keys

Invalid
{name: "John"}
Valid
{"name": "John"}

Every key in JSON must be a double-quoted string, even if it looks like a valid identifier.

4. Missing Comma Between Properties

Invalid
{"name": "John" "age": 30}
Valid
{"name": "John", "age": 30}

Properties must be separated by commas. The parser expects a comma or closing brace after each value.

5. Using undefined or NaN

Invalid
{"value": undefined}
Valid
{"value": null}

JSON does not support undefined, NaN, or Infinity. Use null for missing values.

Other common errors include: 6. Comments in JSON, 7. Mismatched brackets, 8. Control characters in strings, 9. Duplicate keys, and 10. BOM (Byte Order Mark) at the start of the file. Our JSON Formatter can detect and auto-fix many of these errors with one click.

5 Ways to Validate JSON

1. Online JSON Validator (Fastest)

The fastest way to validate JSON is to use an online tool like the ToolZip JSON Formatter & Validator. Paste your JSON, and it instantly shows whether it's valid, highlights the exact error location with line numbers, and offers an auto-fix feature for common issues. Everything runs in your browser — your data is never sent to any server.

2. JSON.parse() in JavaScript

The programmatic approach — wrap in try/catch:

function isValidJSON(str) {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    console.error("Invalid JSON:", e.message);
    return false;
  }
}

3. Command Line (jq)

For terminal users, jq is the gold standard:

# Validate a JSON file
cat data.json | jq .

# Validate and pretty-print
jq . < data.json

# Check if valid (returns exit code 0 or 1)
jq empty data.json && echo "Valid" || echo "Invalid"

4. VS Code Built-in Validation

Visual Studio Code automatically validates .json files and underlines errors with red squiggly lines. Hover over the error to see the description. VS Code also supports JSON Schema validation for structured config files.

5. Python json.loads()

import json

try:
    data = json.loads('{"name": "John", "age": 30}')
    print("Valid JSON")
except json.JSONDecodeError as e:
    print(f"Invalid JSON: {e}")

Using JSON.parse() in JavaScript — Best Practices

When working with JSON in production JavaScript code, always validate before accessing properties:

// BAD: Crashes if response is invalid JSON
const data = JSON.parse(response.body);

// GOOD: Safe parsing with error handling
function safeParse(jsonString) {
  try {
    return { data: JSON.parse(jsonString), error: null };
  } catch (error) {
    return { data: null, error: error.message };
  }
}

const { data, error } = safeParse(response.body);
if (error) {
  console.error("Failed to parse response:", error);
  // Handle error gracefully
}

This pattern prevents unhandled exceptions from crashing your application when an API returns malformed JSON, which happens more often than you might expect — especially with third-party APIs, webhook payloads, and user-submitted data.

JSON Best Practices for APIs

  • Always validate incoming JSON — Never trust JSON from external sources. Validate structure and data types before processing.
  • Use consistent key naming — Pick camelCase or snake_case and stick with it. Our Case Converter can help you switch between naming conventions.
  • Avoid deeply nested structures — Flatten where possible. Deep nesting makes APIs harder to consume and JSON harder to validate.
  • Use null for missing values — Don't omit keys or use empty strings for missing data. null is explicit and unambiguous.
  • Set Content-Type header — Always return Content-Type: application/json from your API. This prevents parsing issues and security vulnerabilities.
  • Pretty-print for debugging, minify for production — Use our JSON Formatter to pretty-print for readability and minify for bandwidth savings.

Frequently Asked Questions

How do I check if JSON is valid?
Paste your JSON into ToolZip's JSON Formatter — it instantly highlights errors with line numbers. Programmatically, use JSON.parse() in JavaScript wrapped in a try/catch block.
What is the most common JSON error?
Trailing commas. For example, {"name": "John",} is invalid. JavaScript objects allow trailing commas, but the JSON specification does not. This trips up developers constantly.
Can JSON have comments?
No. The JSON specification (RFC 8259) does not support comments. Use JSONC (JSON with Comments) in VS Code settings, or switch to YAML or TOML for configuration files that need comments.
What is the difference between JSON.parse() and JSON.stringify()?
JSON.parse() converts a JSON string into a JavaScript object. JSON.stringify() converts a JavaScript object into a JSON string. Use parse() when receiving API data and stringify() when sending it.

Validate Your JSON Now

Paste JSON, get instant validation with error highlighting, tree view, and auto-fix. Free, private, runs in your browser.

Open JSON Formatter & Validator

Related Tools