usefmtly

JSON Formatting Guide

What JSON is, how to read and write it correctly, why formatting matters, and how to fix the errors you will actually encounter.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for storing and transmitting structured data. It was derived from JavaScript but is now language-independent — virtually every programming language can read and write it.

JSON is used everywhere: API responses, configuration files, database exports, log files, and data interchange between services. If you have ever seen a file ending in .json, or inspected network traffic in your browser, you have encountered it.

A minimal JSON object

{
  "name": "Alice",
  "age": 30,
  "active": true,
  "tags": ["developer", "writer"],
  "address": {
    "city": "Paris",
    "country": "France"
  }
}

Beautified vs minified JSON

JSON can be written in two ways: beautified (also called pretty-printed or formatted) and minified (compressed). Both are valid JSON — they contain identical data.

Beautified

{
  "name": "Alice",
  "age": 30,
  "city": "Paris"
}

Minified

{"name":"Alice","age":30,"city":"Paris"}

Use beautified JSON

When reading, editing, debugging, or sharing JSON with other people. Indentation makes nesting levels visible and errors easier to spot.

Use minified JSON

In production APIs and config files where file size matters. Minified JSON reduces payload size, which speeds up network transfers.

JSON syntax rules

Data is in key/value pairs

"name": "Alice"

Keys must be strings wrapped in double quotes. Values can be strings, numbers, booleans, null, arrays, or objects.

Keys must use double quotes

"age": 30 ✓ age: 30 ✗

Single quotes or unquoted keys are not valid JSON, even though they work in JavaScript. This is a common source of parse errors.

Strings use double quotes

"city": "Paris" ✓ "city": 'Paris' ✗

Single-quoted strings are not valid JSON.

No trailing commas

{"a": 1, "b": 2,} ✗

A comma after the last item in an object or array is invalid. This is the most common JSON mistake from developers used to JavaScript or Python.

Numbers are unquoted

"count": 42 ✓ "count": "42" ✗

Wrapping numbers in quotes makes them strings. This matters when code expects a number type for math or comparisons.

Booleans and null are lowercase

"active": true ✓ "active": True ✗

true, false, and null are lowercase keywords. Capitalised versions like True or None (from Python) are not valid.

No comments

// This is not allowed

JSON does not support comments. If you need to annotate JSON, use a field like "_comment" as a workaround, or use a format like JSONC (JSON with Comments) in contexts that support it.

JSON value types

JSON supports exactly six value types. Everything in a JSON document must be one of these:

TypeExampleNotes
String"Hello, world"Must use double quotes. Supports Unicode.
Number42, 3.14, -7, 1.5e3No integer/float distinction. No hex or octal.
Booleantrue, falseLowercase only.
NullnullRepresents an absent or unknown value.
Object{"key": "value"}Unordered set of key/value pairs.
Array[1, "two", true]Ordered list. Can mix types.

Common JSON errors and how to fix them

SyntaxError: Unexpected token

Cause: Usually a missing comma, extra comma, unquoted key, or single-quoted string.

Fix: Check the character position mentioned in the error. Common culprit: trailing comma before a closing bracket.

SyntaxError: Unexpected end of JSON input

Cause: The JSON string is incomplete — a bracket or brace was opened but never closed.

Fix: Check that every { has a matching }, and every [ has a matching ].

Property names must be strings

Cause: An object key is not wrapped in double quotes.

Fix: Wrap all keys in double quotes: name: "Alice" → "name": "Alice".

Invalid number

Cause: Numbers written as .5 (missing leading zero), 1_000 (underscore separator), or with trailing decimal points.

Fix: Write as 0.5, 1000, and 1.0 respectively.

JSON vs related formats

JSON vs YAML

YAML is a superset of JSON that supports comments and uses indentation instead of brackets. YAML is more human-readable but harder for machines to parse reliably. JSON is preferred for APIs; YAML for config files.

JSON vs XML

XML uses tags like HTML and supports attributes, namespaces, and comments. It is more verbose than JSON. Most modern APIs have moved from XML to JSON because JSON is smaller and easier to parse in JavaScript.

JSON vs CSV

CSV is a flat table format — rows and columns, no nesting. JSON supports nested structures, arrays, and mixed types. CSV is better for spreadsheet data; JSON is better for hierarchical or varied data.

Free tools for this