Home / Tech / JSON Formatter
JSON Formatter
JSON Syntax Rules
JSON (JavaScript Object Notation) is a lightweight data format for transmitting structured data. It's strict—one tiny syntax error breaks everything.
- Double quotes only:
"name"not'name' - All keys quoted:
{"name": "John"}not{name: "John"} - No trailing commas:
{"a": 1, "b": 2}not{"a": 1, "b": 2,} - No comments: JSON doesn't support
//or/* */ - 6 data types: String, Number, Boolean, Null, Object, Array (no undefined, NaN, Infinity)
The Trailing Comma Trap
JavaScript allows trailing commas. JSON doesn't. Developers copy-paste JavaScript objects into JSON APIs and get cryptic errors.
{"name": "John", "age": 30}
{"name": "John", "age": 30,}
Error: Unexpected token } in JSON at position 28
💡 Expert Tip: Minify for Production
"Pretty JSON is for humans. Minified JSON is for machines." — Alex Chen, Tech Lead
Whitespace in JSON is cosmetic. Minifying removes spaces, tabs, and newlines:
- 100 KB formatted JSON → 70-80 KB minified (20-30% savings)
- 1M API requests/day → Save 20-30 GB bandwidth/day
- Combine with gzip → Total reduction of 70-90%
Best practice: Store formatted JSON in your editor for debugging, serve minified JSON from APIs for performance.
⚠️ Common Mistakes
1. Using Single Quotes
JavaScript accepts both 'hello'
and "hello",
but JSON only accepts double quotes. This produces: Unexpected token ' in JSON at position 1.
Always use double quotes for strings.
2. Forgetting to Quote Object Keys
JavaScript allows {name: "John"}
(unquoted keys). JSON requires {"name": "John"}.
If you copy JavaScript object literals into JSON, your API will reject them.
3. Quoting Numbers as Strings
{"age": "30"}
creates a string, not a number. When your JavaScript code expects
typeof age === "number", it fails. Numbers don't need quotes: {"age": 30}.
4. Using Undefined, NaN, or Infinity
JSON doesn't support JavaScript's undefined, NaN, or
Infinity. Use null
instead of undefined. For NaN/Infinity, either convert to null or use
quoted strings like "NaN" (then parse manually).
JSON vs JavaScript Object
| Feature | JavaScript Object | JSON |
|---|---|---|
| Quotes | Single or double | Double only |
| Key quotes | Optional | Required |
| Trailing comma | Allowed | Forbidden |
| Comments | Supported | Not supported |
| Data types | undefined, NaN, functions, etc. | 6 types only (no undefined) |