Developer

JSON Formatting and Validation: A Developer's Quick Reference

Format, validate, and understand JSON like a pro. Covers syntax rules, common errors, JSONPath, and practical tools for working with JSON data.

7 min read

Code on screen

JSON (JavaScript Object Notation) is the lingua franca of web APIs. Simple by design — but a single missing comma or mismatched brace can silently break an application. Here is everything you need to work with JSON confidently.

JSON syntax in 90 seconds

JSON has exactly six value types:

{
  "string": "hello world",
  "number": 42,
  "boolean": true,
  "null_value": null,
  "array": [1, 2, 3],
  "object": { "nested": "value" }
}

Rules that trip people up:

  • Keys must be double-quoted strings{name: "Alice"} is not valid JSON
  • No trailing commas — [1, 2, 3,] is invalid
  • No comments — JSON has no // or /* */ syntax
  • Strings must use double quotes — single quotes are not allowed
  • Numbers cannot have leading zeros — 007 is invalid

The most common JSON errors

1. Trailing comma

// Invalid
{ "a": 1, "b": 2, }

// Valid
{ "a": 1, "b": 2 }

2. Single-quoted strings

// Invalid
{ 'name': 'Alice' }

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

3. Unquoted keys

// Invalid
{ name: "Alice" }

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

4. Undefined or NaN values — These JavaScript values don't exist in JSON. Use null instead.

Formatting vs. minifying

Pretty-printed JSON (formatted) uses indentation and newlines for readability:

{
  "user": {
    "id": 1,
    "name": "Alice"
  }
}

Minified JSON strips all whitespace:

{"user":{"id":1,"name":"Alice"}}

Use formatted JSON in config files and source code. Use minified JSON in API responses and storage to reduce payload size. Our JSON Formatter switches between both with one click.

JSON Schema validation

JSON Schema lets you define the expected shape of a JSON document and validate data against it. This is essential for API contracts and configuration file validation.

A simple schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer", "minimum": 0 }
  }
}

This schema rejects any object missing name or age, or where age is negative. The JSON Schema Validator lets you paste schema and data to validate in real time.

Generating TypeScript types from JSON

If you receive a JSON response from an API and need TypeScript interfaces, paste the JSON into our JSON to TypeScript converter. It infers types from the data structure:

Input:

{ "id": 1, "name": "Alice", "active": true, "tags": ["admin"] }

Output:

interface Root {
  id: number;
  name: string;
  active: boolean;
  tags: string[];
}

JSONPath: querying nested data

JSONPath is to JSON what XPath is to XML — a query language for extracting values from nested structures. Basic syntax:

Expression Meaning
$.store.book The book key inside store
$.store.book[0] First book
$.store.book[*].title All book titles
$..price All price values anywhere in the document

The JSON Path Finder lets you click any value in a rendered JSON tree and copies the path automatically.

Comparing two JSON objects

When debugging API changes or config drift, you need to see what changed between two JSON objects. The JSON Diff Viewer highlights added, removed, and modified fields side by side.

Converting JSON to Zod schema

If you use Zod for runtime validation in TypeScript, the JSON to Zod tool converts sample JSON into a ready-to-use Zod schema with smart type inference for email, URL, and datetime fields.

Summary

JSON is simple but unforgiving. Always validate before parsing, use TypeScript interfaces to catch shape mismatches at compile time, and keep a formatter handy for debugging API responses. These habits prevent entire categories of runtime bugs.