JSON Format Example: Well-Formatted JSON, Before and After
A clear JSON format example showing valid syntax, pretty vs minified output, before-and-after fixes for messy JSON, and the most common syntax errors to avoid.
JSON Format Example: Well-Formatted JSON, Before and After
A JSON format example is the fastest way to learn the rules of valid, readable JSON: data is written as key-value pairs inside curly braces, keys are always double-quoted strings, values can be strings, numbers, booleans, null, arrays, or nested objects, and items are separated by commas with no trailing comma allowed. This guide gives you a clean JSON format example, shows the same data before and after formatting, and walks through the syntax errors that trip people up most.
The short version: properly formatted JSON uses consistent indentation (usually two spaces), double quotes around every key, no comments, and no trailing commas. Minified JSON squeezes all of that onto one line to save bytes, while "pretty" JSON expands it for humans to read. To clean up messy JSON instantly, paste it into the free JSON Formatter and get indented, readable output in one click.
The Rules of Valid JSON
JSON (JavaScript Object Notation) is strict by design, which is exactly why it is so portable. A handful of rules cover almost everything you will write.
- Objects are wrapped in
{ }and contain comma-separated key-value pairs. - Arrays are wrapped in
[ ]and contain comma-separated values. - Keys must be strings in double quotes. Single quotes are not valid JSON.
- String values use double quotes too; numbers,
true,false, andnullare written bare. - No trailing comma after the last item, and no comments of any kind.
These constraints are unforgiving on purpose. A missing quote or stray comma makes the entire document invalid, which is why a formatter that also flags errors saves real time.
A Clean JSON Format Example
Here is a well-formatted JSON object representing a product. Notice the two-space indentation, the double-quoted keys, the nested object, and the array of strings.
{
"id": 4821,
"name": "Mechanical Keyboard",
"price": 89.99,
"inStock": true,
"tags": ["peripherals", "wired"],
"dimensions": {
"width": 35.5,
"height": 13.2
},
"discount": null
}
This single example demonstrates every value type JSON supports: an integer (id), a string (name), a float (price), a boolean (inStock), an array (tags), a nested object (dimensions), and null (discount). If you can read this, you can read almost any JSON.
Pretty vs. minified
The same product, minified, removes all whitespace to reduce payload size on the wire.
{"id":4821,"name":"Mechanical Keyboard","price":89.99,"inStock":true,"tags":["peripherals","wired"],"dimensions":{"width":35.5,"height":13.2},"discount":null}
Both versions are identical to a parser; only humans care about the difference. Use minified JSON in API responses and storage, and pretty JSON when you are reading or debugging. A formatter toggles between the two, so the JSON Formatter can expand a minified blob or compress a verbose one on demand.
Before and After: Fixing Messy JSON
Real-world JSON often arrives broken or unreadable. The table below shows common "before" mistakes and the corrected "after" form.
| Before (invalid) | After (valid) | Fix |
|---|---|---|
{name: "Sam"} | {"name": "Sam"} | Quote the key. |
{'city': 'Paris'} | {"city": "Paris"} | Use double quotes, not single. |
{"a": 1, "b": 2,} | {"a": 1, "b": 2} | Remove the trailing comma. |
{"active": True} | {"active": true} | Booleans are lowercase. |
{"note": "line1 // hi"} | {"note": "line1"} | No comments allowed in JSON. |
Every one of these errors produces a hard parse failure. The good news is they are easy to spot once you know the rules, and a formatter highlights the exact line where the document breaks.
The Five Most Common JSON Syntax Errors
- Trailing commas. A comma after the final element or property is invalid JSON, even though JavaScript tolerates it. This is the single most frequent error.
- Single quotes. JSON requires double quotes for both keys and string values; single quotes are a JavaScript habit that JSON rejects.
- Unquoted keys.
{name: "x"}is a JavaScript object literal, not JSON. Keys must be quoted. - Wrong boolean or null casing. JSON uses lowercase
true,false, andnull.TrueorNULLwill fail. - Unescaped characters. Literal newlines, tabs, or unescaped double quotes inside a string break the parse. Escape them as
\n,\t, and\".
If JSON will not parse, scan for these five issues first. They account for the overwhelming majority of real-world failures, and four of the five come from treating JSON like JavaScript.
Indentation, Key Order, and Readability Conventions
Beyond raw validity, good formatting follows conventions that make JSON pleasant to work with across a team. None of these change whether the JSON parses, but they reduce noise in code reviews and diffs.
- Indentation width. Two spaces is the most common choice and keeps deeply nested structures from drifting off the right edge. Four spaces is also fine; tabs are valid but mix poorly across editors. Pick one and apply it consistently.
- Key ordering. JSON objects are unordered by specification, so a parser does not care about key order. For humans and for clean diffs, though, alphabetizing keys (or grouping related keys) makes large objects far easier to scan and compare.
- Consistent array style. Short arrays read well on one line; long arrays are clearer with one element per line. Mixing both styles inside the same file looks careless.
- Encoding. Save JSON as UTF-8 without a byte-order mark. A stray BOM at the start of a file is a surprisingly common cause of a parser rejecting otherwise-perfect JSON.
Because key order does not affect meaning, two files that differ only in ordering are logically identical. Sorting keys before you compare them removes that noise, which is why sort_keys=True in the Python example below is so useful for producing stable, diff-friendly output.
Formatting JSON in Code
You do not always need a tool; standard libraries pretty-print JSON in a line or two. In Python, the indent argument controls formatting.
import json
data = {"id": 4821, "name": "Mechanical Keyboard", "tags": ["wired"]}
# Pretty, human-readable
print(json.dumps(data, indent=2, sort_keys=True))
# Minified for transport
print(json.dumps(data, separators=(",", ":")))
JavaScript does the same with the third argument to JSON.stringify.
const data = { id: 4821, name: "Mechanical Keyboard", tags: ["wired"] };
// Pretty with 2-space indent
console.log(JSON.stringify(data, null, 2));
// Minified
console.log(JSON.stringify(data));
These snippets are perfect inside scripts and build steps. For a quick one-off cleanup of pasted data, though, an online formatter is faster than opening an editor. The browser-based JSON Formatter indents, minifies, and flags errors with no setup, and you can find related utilities in the development tools hub.
Where JSON Formatting Connects to Other Tasks
Clean formatting is usually step one in a larger workflow. Once your JSON is readable, you might validate it against rules, compare two versions, or generate IDs for its records. When two formatted files should be identical but are not, a structural JSON compare shows you precisely which keys differ. If a field holds identifiers, our guide to generating UUIDs in Python helps you populate them. And when you extract or reshape values with text rules, the regex cheat sheet is a handy companion for matching quoted strings and keys.
Frequently Asked Questions
What is a simple JSON format example?
A minimal valid example is {"name": "Sam", "age": 30}: an object with two key-value pairs, double-quoted keys, a string value, a number value, and no trailing comma. From there you can nest objects and arrays to any depth.
What is the difference between pretty and minified JSON?
Pretty JSON uses indentation and line breaks so humans can read it. Minified JSON removes all unnecessary whitespace to reduce size for transport and storage. Both parse identically; the difference is purely for readability versus efficiency.
Are trailing commas allowed in JSON?
No. A comma after the last element in an array or the last property in an object makes the JSON invalid, even though JavaScript object literals allow it. Removing trailing commas fixes a large share of parse errors.
Can JSON have comments?
No. The JSON specification does not support comments of any kind. If you need annotations, use a dedicated comment field as a normal key, or use a format like JSON5 or YAML that allows comments, then convert to strict JSON.
Why must JSON use double quotes?
The JSON specification mandates double quotes for all strings, including keys. Single quotes are valid in JavaScript but not in JSON, so a parser will reject them. Always convert single quotes to double quotes.
How do I format JSON online for free?
Paste your JSON into the browser-based JSON Formatter. It indents messy input, minifies verbose input, and highlights syntax errors instantly, all in your browser with no signup required.
How do I fix invalid JSON quickly?
Check for the five usual culprits: trailing commas, single quotes, unquoted keys, wrong boolean or null casing, and unescaped characters. A formatter that validates as it formats will point you to the exact line where the document fails.