Skip to content

JSON Formatting: Why It Matters and How to Do It Right

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format used by virtually every modern web application. When your browser fetches data from an API, the response is almost certainly JSON. When a mobile app syncs with a server, the payload is JSON. When configuration files store settings, they are increasingly JSON. It has become the universal language of data exchange on the web.

Format, validate, and minify JSON with WritePadPro's JSON Formatter & Validator — paste JSON data and get properly indented, validated output plus a collapsible tree view.

JSON was formalized by Douglas Crockford in the early 2000s as a simpler alternative to XML. Its syntax is derived from JavaScript object notation but is language-independent — every programming language has JSON parsing libraries. Its simplicity (only 6 data types, minimal syntax rules) made it the dominant format for web APIs, where it has largely replaced XML.

JSON Syntax Rules

JSON has strict syntax rules. Unlike JavaScript, there is no room for variation — a single syntax error makes the entire document invalid.

The 6 Data Types

TypeExampleNotes
String"hello"Must use double quotes (single quotes are invalid)
Number42, 3.14, -7No leading zeros, no hex, no NaN/Infinity
Booleantrue, falseLowercase only (True/False are invalid)
NullnullLowercase only (Null/NULL are invalid)
Array[1, 2, 3]Ordered list of values, comma-separated
Object{"key": "value"}Unordered key-value pairs, keys must be strings

The Rules That Trip People Up

  • No trailing commas: {"a": 1, "b": 2,} is INVALID. The comma after the last item is a syntax error.
  • No comments: JSON does not support // comments or /* comments */. Many developers wish it did.
  • Double quotes only: {'key': 'value'} is INVALID. Keys and strings must use double quotes.
  • No undefined: JavaScript's undefined is not a valid JSON value. Use null instead.
  • No functions: JSON is data, not code. Functions cannot be serialized to JSON.
  • Keys must be strings: {42: "value"} is INVALID. Keys must be quoted strings: {"42": "value"}.

WritePadPro's JSON Formatter & Validator validates syntax and points to the first error — line, column, and the surrounding text — in browsers that report a position (Chrome, Edge, and Firefox), making these mistakes easy to find and fix. For generating JSON-LD structured data, see our Schema Markup Guide.

Why Format JSON?

Readability

Raw API responses arrive as single-line minified JSON:

{"users":[{"id":1,"name":"John","email":"[email protected]","roles":["admin","editor"]},{"id":2,"name":"Jane","email":"[email protected]","roles":["viewer"]}]}

Formatted (prettified) with 2-space indentation:

{
  "users": [
    {
      "id": 1,
      "name": "John",
      "email": "[email protected]",
      "roles": ["admin", "editor"]
    },
    {
      "id": 2,
      "name": "Jane",
      "email": "[email protected]",
      "roles": ["viewer"]
    }
  ]
}

The structure is immediately clear: an object with a "users" array containing two user objects, each with id, name, email, and roles.

Debugging

When an API returns unexpected data, formatting reveals the structure. Is the response an array or an object? Is a field missing? Is a value null when it should be a string? These questions are answerable at a glance with formatted JSON — and nearly impossible with minified JSON.

Validation

Formatting tools validate JSON syntax as a side effect of parsing. If the JSON is invalid (missing comma, unquoted key, trailing comma), the formatter reports the error with the line number and character position. This is faster than debugging by reading through hundreds of characters of minified JSON.

Common JSON Errors and How to Fix Them

ErrorInvalid ExampleFixed
Trailing comma{"a": 1, "b": 2,}{"a": 1, "b": 2}
Single quotes{'name': 'John'}{"name": "John"}
Unquoted keys{name: "John"}{"name": "John"}
Comments{"a": 1} // comment{"a": 1} (remove comment)
Undefined value{"a": undefined}{"a": null}
Missing comma{"a": 1 "b": 2}{"a": 1, "b": 2}
Leading zeros{"port": 08080}{"port": 8080}
NaN or Infinity{"val": NaN}{"val": null} or {"val": 0}

WritePadPro's formatter rejects all of these errors, one at a time, and shows where parsing failed in browsers that report a position. For CSS formatting alongside JSON config files, see our CSS Formatting Guide.

JSON Formatting vs Minification

AspectFormatted (Pretty)Minified
PurposeHuman readabilityMachine transmission
WhitespaceIndentation + line breaksNo whitespace
File sizeLarger (depends on nesting and indent size)Smaller (baseline)
Use caseConfig files, debugging, documentationAPI responses, storage, transmission
Parsing speedNearly the same (parsers skip whitespace)Nearly the same

Like CSS, the formatting choice depends on context. Config files (package.json, tsconfig.json, settings files) should be formatted for readability. API responses and stored data should be minified for efficiency.

JSON in Practice

API Responses

Most REST APIs return JSON. GraphQL APIs return JSON. Many modern web services communicate mainly in JSON. When debugging API issues, formatting the response is the first step — it reveals the data structure, identifies missing fields, and shows unexpected values.

Configuration Files

package.json (Node.js), tsconfig.json (TypeScript), .eslintrc.json (ESLint), settings.json (VS Code), manifest.json (PWA/Chrome extensions) — the JavaScript ecosystem runs on JSON configuration. These files are usually stored formatted (often with 2-space indentation) because humans edit them regularly.

Data Storage

Document databases such as MongoDB, CouchDB, and Firebase store data as JSON or JSON-like documents. Log files in JSON Lines format (one JSON object per line) enable structured logging. Data interchange between microservices uses JSON payloads. For building and testing code that produces JSON, use our Code Playground.

Using WritePadPro's JSON Formatter

WritePadPro's JSON Formatter & Validator formats, validates, and minifies JSON data instantly.

Step 1: Paste JSON

Open the JSON Formatter & Validator and paste your JSON data — from API responses, config files, database exports, or any JSON source.

Step 2: Beautify, Minify, or Validate

Click Beautify for readable output with 2-space indentation. Click Minify for compact, single-line output. Click Validate to check the JSON without changing it. If the JSON has a syntax error, the tool shows the browser’s error message and, in Chrome, Edge, and Firefox, the line, column, and surrounding text.

Step 3: Review

The formatted output makes the data structure clear, with nested objects and arrays indented. Switch to Tree View to collapse and expand nested sections and see property and item counts. A stats line shows the line count and size, or how much smaller the minified version is.

Step 4: Copy and Use

Click Copy JSON or Download .json. Use formatted JSON for debugging, documentation, or config files, and minified JSON for API payloads, storage, or transmission.

Privacy

All formatting and validation runs locally in your browser. The tool validates with the built-in JSON.parse() and then re-indents your original text, so even very large numbers keep every digit. Your JSON data — including API responses, database records, and configuration — is never transmitted to any server.

Summary

JSON is the universal data format of the modern web — used in API responses, configuration files, data storage, and inter-service communication. Its strict syntax (double quotes only, no trailing commas, no comments, 6 data types) makes validation essential for debugging.

Formatting (prettifying) JSON makes it human-readable for debugging and configuration. Minifying JSON makes it compact for transmission and storage. Both operations preserve the data identically — only the whitespace changes.

Format, validate, and minify JSON with WritePadPro's JSON Formatter & Validator — instant parsing with error detection, running entirely in your browser.

Frequently Asked Questions

Why does JSON not support comments?

Douglas Crockford, JSON's creator, deliberately excluded comments to prevent misuse. He has said he saw people using comments to hold parsing directives, which would have hurt interoperability between parsers. By excluding comments, JSON remains purely a data format — simple to parse, simple to generate, and impossible to abuse for non-data purposes. For configuration files that need comments, alternatives exist: JSONC (JSON with comments, used by VS Code), JSON5, YAML, and TOML all support comments.

What is the difference between JSON and JavaScript objects?

JSON is a text-based data format. JavaScript objects are in-memory data structures. Key differences: JSON requires double quotes on all keys and strings; JavaScript allows unquoted keys and single quotes. JSON supports only 6 data types; JavaScript objects can contain functions, undefined, Date objects, RegExp, and more. JSON cannot contain comments; JavaScript allows comments. JSON is always a string (text); a JavaScript object exists in memory. JSON.parse() converts JSON text to a JavaScript object; JSON.stringify() converts a JavaScript object to JSON text.

How do I find errors in large JSON files?

Paste the JSON into WritePadPro's JSON Formatter. If the JSON is invalid, the tool shows the error message and, in Chrome, Edge, and Firefox, the line, column, and surrounding text (Safari reports no position). Common errors and how to find them: trailing commas (search for ',}' or ',]'), single quotes (search for single quote characters), unquoted keys (look for keys without double quotes), and missing commas (the parser error usually points to the line after the missing comma). For very large files (10,000+ lines), use a code editor with JSON validation (VS Code highlights errors inline).

Should I use 2-space or 4-space indentation for JSON?

Two-space indentation is a very common convention for JSON. Files in the JavaScript ecosystem (package.json, tsconfig.json, .eslintrc.json) usually use 2 spaces. JSON.stringify(data, null, 2) indents with the number of spaces you pass, and 2 is a common choice. Four-space indentation is acceptable but less common for JSON specifically. The choice matters less than consistency — pick one and use it everywhere. WritePadPro's JSON Formatter always uses 2-space indentation when you click Beautify.

Try These Tools

Related Articles