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.
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 after2. - 4. No comments — JSON does not support
// commentsor/* block comments */. - 5. Supported data types — strings, numbers, booleans (
true/false),null, arrays, and objects. Noundefined,NaN,Infinity, or functions. - 6. Numbers cannot have leading zeros —
007is invalid. Use7.
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
{"name": "John", "age": 30,}{"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
{'name': 'John'}{"name": "John"}JSON requires double quotes for all strings and keys. Single quotes are a JavaScript convention, not valid JSON.
3. Unquoted Keys
{name: "John"}{"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
{"name": "John" "age": 30}{"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
{"value": undefined}{"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
camelCaseorsnake_caseand 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.
nullis explicit and unambiguous. - Set Content-Type header — Always return
Content-Type: application/jsonfrom 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?
What is the most common JSON error?
Can JSON have comments?
What is the difference between JSON.parse() and JSON.stringify()?
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