JSON Formatter Guide: How JSON Works & Common Errors Explained
Quick Answer
- *JSON (JavaScript Object Notation) is a lightweight text format for storing and transmitting structured data as key-value pairs and arrays.
- *It has 6 data types: string, number, boolean, null, object, and array — nothing else.
- *Use pretty-print for readability when debugging; use minified JSON in production to reduce payload size.
- *The 5 most common JSON errors are trailing commas, unquoted keys, single-quoted strings, missing brackets, and invalid escape sequences.
What Is JSON?
JSON stands for JavaScript Object Notation. It's a plain-text format for representing structured data. Despite the name, JSON has nothing to do with JavaScript at runtime — it's a language-independent serialization format specified by json.org and formally defined in RFC 8259.
A minimal JSON object looks like this: {"name": "Alice", "age": 30}. Keys are always strings in double quotes. Values can be any of the 6 supported types.
According to the Stack Overflow Developer Survey, JSON is the most commonly used data format among developers worldwide, with REST APIs — which almost exclusively use JSON — powering over 83% of public API integrationsas of 2024. JSON has largely displaced XML in web services because it's more compact, easier to read, and natively parseable in every modern programming language.
The 6 JSON Data Types
JSON supports exactly six value types. No dates. No functions. No undefined. No comments. This strictness is intentional — it keeps parsers simple and interoperable across languages.
| Type | Example | Notes |
|---|---|---|
| String | "hello world" | Must use double quotes. Single quotes are invalid. |
| Number | 42, 3.14, -7 | Integer or floating point. No hex, no NaN, no Infinity. |
| Boolean | true, false | Lowercase only. True is a syntax error. |
| Null | null | Lowercase only. Represents an empty or absent value. |
| Object | {"key": "value"} | Unordered key-value pairs. Keys must be strings. |
| Array | [1, "two", true] | Ordered list. Values can be any type, including mixed. |
Objects and arrays can nest arbitrarily deep. A typical API response might have objects inside arrays inside other objects — that's all valid JSON as long as every value is one of these six types.
JSON Syntax Rules
JSON has fewer rules than most formats, but they're rigid. Break any one of them and your JSON is invalid.
- Keys must be strings in double quotes.
{name: "Alice"}is not valid JSON.{"name": "Alice"}is. - Strings use double quotes only. Single quotes like
{'name': 'Alice'}are a syntax error. - No trailing commas.
[1, 2, 3,]is invalid. The last item in an array or object cannot have a trailing comma. - No comments. JSON has no comment syntax. Adding
// commentor/* comment */breaks parsing. - Numbers cannot be NaN or Infinity. These JavaScript values have no JSON equivalent.
- The root value can be any type. A JSON document can be a single string, number, array, or object — not just an object.
These rules come directly from the RFC 8259 specification. Any compliant JSON parser will reject input that violates them.
Pretty-Print vs Minified JSON
The same JSON data can be represented in two ways: pretty-printed (human-readable) or minified (compact). Both contain identical data. The only difference is whitespace.
Pretty-printed:
{
"user": {
"name": "Alice",
"age": 30,
"active": true
}
}Minified:
{"user":{"name":"Alice","age":30,"active":true}}| Format | Use When | Tradeoff |
|---|---|---|
| Pretty-print | Debugging, code review, API docs, config files | Larger payload; easier to read |
| Minified | Production API responses, network transfer, storage | Smaller payload; harder to read without a formatter |
In practice, minifying JSON can reduce payload size by 15–30% on typical API responses. For large datasets transferred frequently, this adds up. Most backend frameworks minify JSON output by default in production mode. Use our JSON Formatter to toggle between the two instantly.
5 Common JSON Errors and How to Fix Them
These are the errors that trip up developers most often. Every one of them will cause a SyntaxError or parsing failure.
1. Trailing Commas
Invalid: [1, 2, 3,] or {"a": 1, "b": 2,}
Fix:Remove the comma after the last element. The RFC 8259 spec explicitly forbids trailing commas. This is the single most common JSON error — especially from developers who write JavaScript (which allows trailing commas in JS arrays and objects) and copy-paste into JSON.
2. Unquoted Keys
Invalid: {name: "Alice"}
Fix: Quote every key: {"name": "Alice"}. JavaScript object literals allow unquoted keys. JSON does not. Every key must be a double-quoted string.
3. Single-Quoted Strings
Invalid: {"name": 'Alice'}
Fix: Use double quotes: {"name": "Alice"}. Single-quoted strings are valid in JavaScript, Python, and many other languages — but not in JSON. Both keys and string values must use double quotes.
4. Missing or Mismatched Brackets
Invalid: {"items": [1, 2, 3} (array never closed)
Fix: Every opening { needs a closing }, and every [ needs a ]. Use a formatter to auto-detect mismatched brackets. This error is most common in deeply nested JSON that's written manually or truncated.
5. Invalid Escape Sequences
Invalid: {"path": "C:\Users\Alice"}
Fix: Escape backslashes: {"path": "C:\\Users\\Alice"}. JSON supports only these escape sequences inside strings: \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX. Any other backslash sequence is invalid.
JSON vs XML vs YAML
JSON isn't the only data serialization format, but it's the most widely used for web APIs. Here's how it compares:
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Human-readable | Good | Verbose | Excellent |
| Comments supported | No | Yes | Yes |
| Data types | 6 native types | All strings (with schema) | Rich types + dates |
| Payload size | Compact | Large (tag overhead) | Compact |
| Common use | REST APIs, web apps | Enterprise, SOAP, config | Config files, CI/CD |
| Parse complexity | Simple | Complex | Complex (indent-sensitive) |
JSON won the web API battle because it's small, fast to parse, and directly maps to data structures in every major programming language. XML still dominates in enterprise legacy systems and document-heavy formats. YAML is preferred for configuration files because it supports comments and is easier to write by hand. Need to convert between them? Try our JSON to YAML Converter.
How to Format JSON in Common Languages
Every major language has built-in JSON support. Here's how to pretty-print JSON in the most common ones:
JavaScript/Node.js: JSON.stringify(obj, null, 2)— the third argument sets indent spaces.
Python: json.dumps(obj, indent=2)
curl + jq (command line): curl https://api.example.com/data | jq .
Go: json.MarshalIndent(obj, "", " ")
For quick formatting without writing code, paste your JSON directly into our free JSON Formatter. It handles pretty-printing, minifying, and validation in one step.
Working with JSON in APIs
When calling a REST API, JSON shows up in two places: the request body and the response body. A Content-Type: application/json header tells the server you're sending JSON. A Accept: application/json header tells the server you want JSON back.
A typical POST request body might look like:
{
"username": "alice",
"email": "[email protected]",
"role": "admin"
}When debugging API responses, a formatter is essential. Raw minified JSON from a production endpoint is nearly impossible to read. Paste it into a formatter to instantly see the structure. You can also encode and decode JSON values that travel as URL parameters using our URL Encoder/Decoder, or handle base64-encoded JSON payloads with our Base64 Encoder/Decoder.
JSON Schema Validation
JSON Schema is a vocabulary for annotating and validating JSON documents. It lets you define what a valid JSON structure looks like — required fields, value types, string patterns, numeric ranges, and more.
A simple JSON Schema that validates a user object:
{
"type": "object",
"required": ["name", "age"],
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
}
}JSON Schema is supported natively by most API frameworks and can be used in CI pipelines to catch data contract violations before they reach production. The JSON Schema specification is maintained at json-schema.org and is widely adopted across the industry.
JSON Security Considerations
JSON itself is just text — it's not executable and carries no inherent security risk when properly parsed. But there are a few patterns to watch for:
- Never use eval() to parse JSON. Use
JSON.parse()in JavaScript.eval()executes arbitrary code. - Validate and sanitize API inputs. Even valid JSON can contain malicious strings designed for SQL injection or XSS if values are passed directly to other systems.
- Watch for prototype pollution. Merging untrusted JSON objects in JavaScript can overwrite
Object.prototypeproperties. Use libraries that protect against this. - Limit nesting depth. Deeply nested JSON (hundreds of levels deep) can cause stack overflow errors in recursive parsers. Most production APIs cap at 20–32 levels.
Format, validate, and debug JSON instantly
Try our free JSON Formatter →Also useful: Base64 Encoder/Decoder, Regex Tester, URL Encoder/Decoder
Frequently Asked Questions
What is JSON used for?
JSON is the standard data format for REST APIs, web services, and configuration files. It transmits structured data between servers and clients, stores app settings, and powers almost every modern web and mobile application. Over 70% of public APIs use JSON as their primary data format.
What are the 6 JSON data types?
JSON supports exactly 6 data types: string (text in double quotes), number (integer or decimal), boolean (true or false), null (empty value), object (key-value pairs in curly braces), and array (ordered list in square brackets). No other types — no dates, no functions, no undefined.
What is pretty-print JSON?
Pretty-print JSON adds indentation and line breaks to make the structure human-readable. It uses 2 or 4 spaces per level. Use it when debugging, reviewing, or documenting APIs. Use minified (compact) JSON in production to reduce file size and improve transfer speed.
Why does JSON not allow trailing commas?
The JSON specification (RFC 8259) was designed to be simple and unambiguous. A trailing comma after the last item in an array or object — like [1, 2, 3,]— was excluded to avoid edge-case parsing ambiguity. Most JSON parsers will throw a SyntaxError if they encounter one.
What is the difference between JSON and JavaScript objects?
JSON looks like a JavaScript object but has stricter rules. JSON keys must be double-quoted strings. JSON does not support functions, undefined, comments, or single quotes. A JavaScript object is a runtime value; JSON is a text serialization format that can be used in any language.
How do I validate JSON online?
Paste your JSON into a free JSON formatter and validator tool. It will parse the text and report any syntax errors with line and column numbers. Common errors include trailing commas, unquoted keys, single-quoted strings, and missing closing brackets or braces.