A Developer Guide to JSON Formatting and Validation

DevToolkit Team · · 9 min read

JSON (JavaScript Object Notation) is the most widely used data format on the web. Every REST API, every configuration file, every data pipeline touches JSON at some point. Yet developers routinely lose time to formatting issues, validation errors, and syntax mistakes that are easy to avoid.

This guide covers everything you need to know about working with JSON effectively — from common errors to validation best practices.

JSON Syntax Rules (The Complete List)

JSON's syntax is intentionally minimal. Here are all the rules:

The 5 Most Common JSON Errors

1. Trailing Commas

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

This is valid JavaScript but invalid JSON. Remove the comma after the last property. Every JSON parser will reject this.

2. Single Quotes

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

JSON requires double quotes for all strings and keys. No exceptions.

3. Unquoted Keys

// INVALID — unquoted key
{ name: "Alice" }

Every key must be a double-quoted string, even if it's a simple identifier.

4. Comments

// INVALID — comments not allowed
{
  "name": "Alice", // user's name
  "age": 30
}

JSON does not support comments. If you need comments in config files, consider JSONC (JSON with Comments) or switch to YAML/TOML. Read our JSON vs YAML vs TOML comparison for guidance.

5. Missing Quotes Around String Values

// INVALID — unquoted string value
{ "status": active }

String values must be quoted: "status": "active". Without quotes, the parser interprets active as an undefined identifier.

JSON Formatting Best Practices

Use 2-Space Indentation

The JavaScript ecosystem has standardized on 2-space indentation for JSON. It's compact enough to keep deeply nested structures readable:

JSON.stringify(data, null, 2)

Keep Keys Consistent

Pick one naming convention for keys and stick with it. camelCase is standard in JavaScript APIs. snake_case is common in Python and Ruby APIs. Mixing conventions makes code harder to work with.

Sort Keys for Diffs

When JSON files are committed to version control, sorted keys produce cleaner diffs:

JSON.stringify(data, null, 2) // unsorted — key order depends on insertion
JSON.stringify(data, Object.keys(data).sort(), 2) // sorted — stable diffs

Minify for Production

Remove whitespace from JSON in API responses to reduce payload size. A typical JSON response is 30-50% smaller when minified:

JSON.stringify(data) // minified — no whitespace

Use our JSON Formatter to switch between formatted and minified views instantly.

JSON Validation Methods

Online Validators

For quick checks, paste your JSON into an online validator. Our JSON Formatter & Validator highlights syntax errors with line numbers and runs entirely in your browser — your data stays private.

JSON Schema

For API contracts and configuration validation, JSON Schema is the standard. It defines the expected structure, types, and constraints:

{
  "type": "object",
  "required": ["name", "email"],
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "email": { "type": "string", "format": "email" },
    "age": { "type": "integer", "minimum": 0 }
  }
}

Libraries like Ajv (JavaScript), jsonschema (Python), and many others validate data against JSON Schema at runtime.

TypeScript Type Guards

In TypeScript projects, use type guards or libraries like Zod to validate JSON at runtime while maintaining type safety:

import { z } from 'zod';

const UserSchema = z.object({
  name: z.string(),
  email: z.string().email(),
  age: z.number().int().positive().optional(),
});

const user = UserSchema.parse(jsonData); // throws if invalid

Working with Large JSON

JSON Alternatives

JSON isn't always the best choice:

Conclusion

JSON's simplicity is its greatest strength. Master the syntax rules, validate early, and choose the right tool for each job. For quick formatting and validation, our JSON Formatter & Validator is always one click away.

Enjoyed this article?

Get the free Developer Cheatsheet Pack + weekly tips on tools, workflows, and productivity.

Subscribe Free

Try These Tools

Related free tools mentioned in this article

Back to Blog