100% Private

JSON Validation: A Complete Developer's Guide

Master JSON validation with this comprehensive guide covering syntax rules, common errors, debugging techniques, and best practices for working with JSON data.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the de facto standard for web APIs, configuration files, and data storage. It's human-readable, language-independent, and easy to parse.

JSON was derived from JavaScript but is now used across virtually every programming language. Its simplicity and flexibility make it ideal for:

  • API responses: RESTful APIs commonly return JSON data
  • Configuration files: package.json, tsconfig.json, etc.
  • Data storage: NoSQL databases like MongoDB
  • Data exchange: Between services and applications

JSON Syntax Rules

Valid JSON must follow strict syntax rules. Understanding these rules is essential for writing and debugging JSON:

Data Types

JSON supports six data types:

TypeExampleNotes
String"hello world"Must use double quotes
Number42, 3.14, -17No quotes, no leading zeros
Booleantrue, falseLowercase only
NullnullLowercase only
Array[1, 2, 3]Ordered list of values
Object{"key": "value"}Unordered key-value pairs

Key Rules to Remember

  • Double quotes only: Strings and object keys must use double quotes ("), never single quotes
  • No trailing commas: The last item in an array or object cannot have a trailing comma
  • No comments: JSON does not support comments (use JSON5 or JSONC if you need them)
  • No undefined: Use null instead of undefined
  • UTF-8 encoding: JSON files should be UTF-8 encoded

Valid JSON Example

{
"name": "John Doe",
"age": 30,
"isActive": true,
"email": null,
"hobbies": ["reading", "coding", "gaming"],
"address": {
"city": "New York",
"zipCode": "10001"
}
}

Common JSON Errors

These are the most frequent mistakes that break JSON validation:

1. Single Quotes Instead of Double Quotes

// ❌ Invalid
{'name': 'John'}

// ✅ Valid {"name": "John"}

2. Trailing Commas

// ❌ Invalid
{
"name": "John",
"age": 30,
}

// ✅ Valid { "name": "John", "age": 30 }

3. Missing Quotes Around Keys

// ❌ Invalid
{name: "John"}

// ✅ Valid {"name": "John"}

4. Using JavaScript Literals

// ❌ Invalid
{
"callback": function() {},
"value": undefined,
"date": new Date()
}

// ✅ Valid { "callback": null, "value": null, "date": "2024-01-15T10:30:00Z" }

5. Unescaped Special Characters

// ❌ Invalid
{"message": "She said "Hello""}

// ✅ Valid {"message": "She said "Hello""}

Validation Methods

Browser-Based Validation

For quick validation without installing anything, use our JSON Validator. It runs entirely in your browser, so your data stays private.

JavaScript/Node.js

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

// Usage const result = validateJSON('{"name": "John"}'); console.log(result); // { valid: true }

Python

import json

def validate_json(json_string): try: json.loads(json_string) return True except json.JSONDecodeError as e: print(f"Invalid JSON: {e}") return False

Command Line (jq)

# Validate JSON file
cat data.json | jq . > /dev/null && echo "Valid" || echo "Invalid"

# Pretty print if valid jq . data.json

Debugging Tips

1. Use a Formatter First

Minified JSON is hard to debug. Use our JSON Formatter to add proper indentation before looking for errors.

2. Check Line Numbers

Most JSON parsers report the line and column where the error occurred. Look at that specific location and the lines before it.

3. Binary Search for Errors

For large JSON files, remove half the content and validate. If it's valid, the error is in the removed half. Repeat until you find the issue.

4. Watch for Invisible Characters

Copy-pasting from word processors or PDFs can introduce invisible characters. Paste into a plain text editor first.

5. Use a Tree Viewer

Our JSON Viewer displays JSON as an interactive tree, making it easier to spot structural issues.

Best Practices

For API Responses

  • Always set Content-Type: application/json header
  • Use consistent naming conventions (camelCase or snake_case)
  • Include proper error responses with error codes and messages
  • Use ISO 8601 format for dates: "2024-01-15T10:30:00Z"

For Configuration Files

  • Use JSON5 or JSONC if you need comments
  • Keep configurations flat when possible
  • Use environment-specific files (config.dev.json, config.prod.json)
  • Validate configuration on application startup

For Data Storage

  • Consider JSON Schema for validation
  • Use consistent data types (don't mix strings and numbers for IDs)
  • Document your JSON structure
  • Version your data format if it evolves

Recommended Tools

Here are free tools to help you work with JSON:

JSON Validator

Check JSON syntax and see detailed error messages.

Validate JSON
JSON Formatter

Beautify minified JSON with customizable indentation.

Format JSON
JSON Minifier

Compress JSON by removing whitespace.

Minify JSON
JSON Viewer

Explore JSON with an interactive tree view.

View JSON

Quick Reference Card

Valid JSON
  • Double quotes for strings: "text"
  • No trailing commas
  • Boolean: true/false (lowercase)
  • Null: null (lowercase)
  • Numbers without quotes
Invalid JSON
  • Single quotes: 'text'
  • Trailing commas: [1, 2, 3,]
  • Comments: // or /* */
  • Undefined values
  • Unquoted keys: {key: "value"}

Last updated: December 2024

All JSON tools on ToolsDock process data entirely in your browser. Your data never leaves your device.

Privacy Notice: This site works entirely in your browser. We don't collect or store your data. Optional analytics help us improve the site. You can deny without affecting functionality.