JSON Formatter & Validator — Beautify, Minify, Sort Keys
JSON errors are a daily frustration for developers. A missing comma, an unquoted key, or a minified API response can stall debugging for minutes. This tool solves all three problems in one place: paste any JSON string and instantly see whether it is valid, formatted with the indentation style you prefer, or compressed to its minimum size. You can sort object keys alphabetically for deterministic diffs in version control, and the tool detects common schema types (OpenAPI, JSON Schema, Swagger) so you know what you are working with at a glance. File size is shown before and after so you can quantify the savings from minification. All processing happens entirely in your browser — your JSON is never transmitted to any server, making this safe to use with API keys, credentials, or other sensitive payloads. No account required. No file size limit beyond your device's memory.
JSON Syntax Rules (RFC 8259 Essentials)
JSON is defined by RFC 8259, which specifies exactly six value types:
- string — UTF-8 text wrapped in double quotes:
"hello" - number — integer or floating-point:
42,3.14,-1e10 - boolean — lowercase only:
trueorfalse - null — lowercase only:
null - object — unordered set of key/value pairs:
{"key": value}. Keys must be strings (double-quoted). - array — ordered list of values:
[1, "two", true, null]
Objects and arrays can nest to any depth. The top-level value can be any of the six types — it does not
have to be an object. "hello" and [1,2,3] are both valid JSON documents.
The two most commonly violated rules are: (1) no trailing commas — the last item in
an object or array must not have a comma after it; and (2) no comments — neither
// nor /* */ syntax is permitted. Both are valid in JavaScript, which is why
developers who write JSON by hand often trip over them.
Common JSON Mistakes That Cause Parse Errors
Every JSON parse error maps to one of a small set of mistakes. Knowing them in advance saves considerable debugging time.
- Single quotes instead of double quotes
'name': 'Alice'is JavaScript, not JSON. Must be"name": "Alice". Both the key and the string value must use double quotes. - Trailing commas
{"a": 1, "b": 2,}— the comma after2is illegal in JSON. This is the single most common mistake when editing JSON by hand, because JavaScript object literals and most modern languages allow trailing commas. - Unquoted keys
{name: "Alice"}— the keynamemust be quoted:{"name": "Alice"}. - Comments
// this is a commentand/* block */both cause a parse error. Remove comments or switch to a format that supports them (JSON5, YAML, TOML). - Missing commas between items
["a" "b" "c"]— commas are required between every pair of array elements or object properties. Missing one produces an "unexpected string" error. - Unescaped quotes inside strings
"He said "hello""— the inner double quotes must be escaped:"He said \"hello\"". Other characters requiring escaping include backslash (\\), forward slash (\/, optional), and control characters (\n,\t,\r, etc.). - Non-JSON values: undefined, NaN, Infinity, functions
JavaScript'sundefined,NaN,Infinity, and-Infinityare not valid JSON values.JSON.stringifysilently drops object properties withundefinedvalues and replacesNaN/Infinitywithnull.
Worked Example: Fixing Invalid JSON
Here is a broken JSON payload copied from a code review comment and the step-by-step repair:
Original (invalid):
{
name: 'Alice', // single-quote string keys
"age": 30,
"roles": ["admin",], // trailing comma in array
// TODO: add email // comment not allowed
"active": undefined // not a JSON value
}
Step 1 — Remove comments (lines starting with //).
Step 2 — Replace single quotes with double quotes: name → "name", 'Alice' → "Alice".
Step 3 — Remove trailing comma after "admin".
Step 4 — Replace undefined with null (or remove the property).
Fixed (valid):
{
"name": "Alice",
"age": 30,
"roles": ["admin"],
"active": null
} Formatting Options — When to Use Each
The formatter supports four indentation modes. Choose based on your project's convention:
- 2 spaces — the default in most JavaScript and Node.js projects. Used by
JSON.stringify(obj, null, 2). Output from npm, package.json, and the majority of JS tooling uses 2-space indentation. - 4 spaces — common in Python projects (PEP 8 style) and many Java or C# codebases.
Also the default in Python's
json.dumps(obj, indent=4). - Tab — preferred by some teams for accessibility (tab width is user-configurable in editors) and is the default in some older PHP and WordPress configs.
- Minify — removes all unnecessary whitespace to produce the smallest possible JSON string. Use for API responses, localStorage values, database columns, or any context where size matters more than readability.
Key Sorting — Why It Matters
By default, JSON.stringify preserves the insertion order of object keys. This means
two JSON objects representing the same data can look different if the keys were inserted in different
order, making git diff noisy and automated comparison unreliable.
Sorting keys alphabetically solves these problems:
- Deterministic version control diffs — if an API adds a new key, the diff shows only that addition rather than a scrambled reordering of unrelated keys.
- Deduplication — two JSON blobs with the same data but different key order become byte-for-byte identical after sorting, making deduplication trivial.
- API response comparison — when comparing responses across API versions or environments, sorted output makes structural differences immediately obvious.
- Caching and ETags — sorted, minified JSON produces a stable string that can be hashed into a reliable cache key or ETag value.
Note: JSON objects are technically unordered by specification (RFC 8259 §4), so sorting keys does not change semantics — it only affects the serialized string representation.
Size and Performance — How Much Does Minification Save?
Formatting adds whitespace that is purely for human readability. Minification strips it. The savings depend on how deeply nested the JSON is and how many properties it contains.
A typical REST API response with 10 KB of formatted JSON (2-space indent) compresses to around 6–7 KB minified — a 30–40% reduction. Deeply nested structures with many short values can save 50–60%. The formatter shows you exact byte counts so you can see the real saving for your data.
After minification, gzip compression (applied automatically by most web servers and CDNs) reduces the payload further — typically by another 60–70%. So a 10 KB formatted JSON might become 7 KB minified then 2–3 KB gzipped. The combination of minification and gzip is standard practice for production APIs.
For client-side storage (localStorage, IndexedDB, cookies), gzip is not applied automatically, so minification alone directly reduces the stored byte count.
Parsing speed is not meaningfully affected by whitespace — both the minified and formatted strings parse at the same rate because parsers skip whitespace in constant time.
Privacy & Security
JSON is parsed and formatted entirely in your browser using the native JSON.parse()
and JSON.stringify() APIs. Your input never touches our server. There
are no network requests triggered when you click Format, Minify, or Validate. You can verify this
by opening browser DevTools > Network and observing no outbound requests during formatting.
This makes the tool safe to use with sensitive payloads including API keys, access tokens, personal data, or internal system configurations. The tool does not persist any input between page loads — refreshing the page clears the textarea.
The one browser API used beyond JSON is navigator.clipboard.writeText() for the Copy
button. This sends your formatted output to your local clipboard only — it is not transmitted anywhere.
Modern browsers require HTTPS for clipboard access; if you are running this locally over HTTP, the
Copy button may not work, but you can select and copy the output textarea manually.
Frequently Asked Questions
Is JSON the same as a JavaScript object literal?
No. JSON (JavaScript Object Notation) is a strict text format defined by RFC 8259. While it looks similar to a JavaScript object literal, there are key differences: JSON requires double-quoted string keys on every property (not optional), it does not allow trailing commas, it has no support for comments, and it cannot represent values like undefined, Infinity, NaN, or functions. A JavaScript object literal is source code with much looser rules. JSON5 is a separate format that relaxes some of those restrictions.
Why can't I use comments in JSON?
JSON was deliberately designed without comments by Douglas Crockford. His reasoning was that JSON is a data interchange format, not a config format — if comments were allowed, people would use them to hold parsing directives, defeating the purpose of a simple universal format. If you need comments in a JSON-like config file, consider JSON5 (supports //, /* */), JSONC (used by VS Code), or YAML. For configuration in code projects, many tools accept jsonc files alongside standard json.
What is JSONP and how is it different?
JSONP (JSON with Padding) is a historical browser hack to work around the same-origin policy that prevented browsers from making cross-domain XHR requests. A server wraps its JSON response in a function call — e.g. callback({"key":"value"}) — and the browser loads it as a script tag, calling the named function when it executes. JSONP is not standard JSON and this formatter will reject it (it is valid JavaScript, not valid JSON). JSONP has been largely replaced by CORS (Cross-Origin Resource Sharing) headers and is considered legacy.
Is JSON safe to parse from user input?
The native JSON.parse() method in modern browsers and Node.js is safe — it does not execute code. Unlike the old eval() approach, JSON.parse() cannot run arbitrary JavaScript. However, be aware of prototype pollution attacks: if a JSON payload contains a key named "__proto__", "__defineGetter__", or "constructor", a naive recursive merge function could pollute the object prototype chain. Always validate the shape of untrusted JSON against a schema (JSON Schema, Zod, Yup) before using the parsed data in your application.
What is the difference between JSON and JSON5?
JSON5 is a superset of JSON that relaxes several restrictions to make hand-authoring easier. JSON5 allows: single-quoted strings, trailing commas in objects and arrays, unquoted property names (if they are valid identifiers), comments (// and /* */), multi-line strings via backslash escaping, and hexadecimal number literals. JSON5 is not natively supported by browsers or Node.js — you need the json5 npm package. It is commonly used for configuration files (e.g., some Babel and TypeScript configs).
How do I handle very large JSON files?
Browser-based tools including this one load the entire JSON string into memory, which works fine up to a few megabytes. For very large files (50 MB+), the browser may become slow or run out of memory. Instead, use command-line tools: jq is the gold standard (e.g. jq . large.json > formatted.json), Python's json.tool module (python -m json.tool large.json), or Node.js (node -e "process.stdout.write(JSON.stringify(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')),null,2))"). For streaming multi-gigabyte files, use a streaming JSON parser like stream-json for Node.js.