JSON Syntax Guide
Everything you need to know about JSON — data types, formatting rules, common mistakes, comparison with YAML and XML, and JavaScript usage examples.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and transmitting structured data. Despite its name, JSON is language-independent and is supported natively by every major programming language.
JSON is used everywhere: REST APIs, configuration files, databases (MongoDB, Firestore), environment settings, and data exchange between services. If you have worked with an API, you have worked with JSON.
{
"name": "Alice",
"age": 30,
"active": true,
"tags": ["developer", "designer"],
"address": {
"city": "Tokyo",
"country": "Japan"
},
"nickname": null
}JSON Data Types
JSON supports exactly six data types. Every value in a JSON document must be one of these.
"Hello, world!"Must use double quotes. Supports escape sequences: \" \n \t \\
42 or 3.14 or -7 or 1e10No distinction between integer and float. No NaN or Infinity.
true or falseLowercase only. Uppercase TRUE is not valid JSON.
nullRepresents the intentional absence of a value. Lowercase only.
{ "key": value }Unordered key-value pairs. Keys must be double-quoted strings.
[value1, value2]Ordered list of values. Can mix types: [1, "two", true, null].
{
"string": "hello",
"number": 42,
"boolean": true,
"null": null,
"object": { "nested": "value" },
"array": [1, 2, 3]
}JSON Syntax Rules
JSON has strict syntax requirements. Even a single violation causes a parse error.
{ name: "Alice" } // Single quotes or unquoted{ "name": "Alice" } // Double quotes required{ "name": 'Alice' } // Single-quoted string{ "name": "Alice" } // Double-quoted string{ "a": 1, "b": 2, } // Trailing comma after last item{ "a": 1, "b": 2 } // No trailing comma{ "a": 1 // this is a // Comments not allowed
comment }{ "a": 1 } // Use a separate docs file{ "msg": "say "hi"" } // Unescaped quotes{ "msg": "say \"hi\"" } // Escaped with backslashTop 5 Common JSON Errors
These are the mistakes that cause most JSON parse failures in practice.
Adding a comma after the last item in an object or array. JavaScript allows this; JSON does not.
[
"apple",
"banana", ← trailing comma
][
"apple",
"banana"
]Using single quotes instead of double quotes around strings or keys.
{ 'name': 'Alice' }{ "name": "Alice" }Object keys that look like JavaScript identifiers work in JS objects but are invalid in JSON.
{ name: "Alice", age: 30 }{ "name": "Alice", "age": 30 }JavaScript's undefined, NaN, and Infinity are not valid JSON values.
{ "value": undefined,
"ratio": NaN }{ "value": null,
"ratio": 0 }Newlines, tabs, and quotes inside strings must be escaped with a backslash.
{ "note": "line one
line two" }{ "note": "line one\nline two" }JSON vs YAML vs XML
Choosing a data format depends on your context — API responses, config files, or legacy systems each have different trade-offs.
| Feature | JSON | YAML | XML |
|---|---|---|---|
| Human readability | Good | Excellent | Poor |
| Comments supported | No | Yes | Yes |
| Trailing commas | Not allowed | N/A | N/A |
| Data types | 6 native | Richer (dates, etc.) | Strings only (without schema) |
| Verbosity | Low | Very low | High |
| Browser support | Native | Needs library | Needs library |
| Best for | APIs, databases | Config files | Enterprise / legacy |
JSON in JavaScript
JavaScript provides two built-in methods for working with JSON.
JSON.parse() — string to object
const jsonString = '{"name":"Alice","age":30}';
const user = JSON.parse(jsonString);
console.log(user.name); // "Alice"
console.log(user.age); // 30
// Always wrap in try/catch for user input
try {
const data = JSON.parse(untrustedInput);
} catch (err) {
console.error("Invalid JSON:", err.message);
}JSON.stringify() — object to string
const user = { name: "Alice", age: 30, active: true };
// Compact (for APIs)
const compact = JSON.stringify(user);
// → '{"name":"Alice","age":30,"active":true}'
// Pretty-printed (for logging)
const pretty = JSON.stringify(user, null, 2);
// → {
// "name": "Alice",
// "age": 30,
// "active": true
// }
// Exclude undefined values automatically
const obj = { a: 1, b: undefined };
JSON.stringify(obj); // → '{"a":1}'JSON Best Practices
Choose one convention and stick to it. camelCase ("firstName") is standard for JavaScript APIs; snake_case ("first_name") is common in Python and databases.
More than 3–4 levels of nesting makes JSON hard to read and query. Flatten structures or use references (IDs) to related objects.
Prefer arrays over numbered object keys. {"0": "a", "1": "b"} is a code smell — use ["a", "b"] instead.
Use null for intentionally missing values, and omit the key entirely for optional fields that are not applicable. Document the difference.
Never trust JSON from external sources without validation. Use a schema validator (Zod, AJV, Joi) to enforce structure and types.
Flat JSON is faster to parse and easier to map to UI components. Use query parameters to request only the fields you need.
Put JSON knowledge to work
Format, validate, and convert JSON right in your browser — no setup required.
JSON FAQ
Answers to common questions about JSON syntax, usage, and tooling.
What is JSON?+
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format. It is easy for humans to read and write, and easy for machines to parse and generate. JSON is derived from JavaScript object literal syntax but is language-independent and supported by virtually every programming language.
Does JSON support comments?+
No. The JSON specification does not allow comments. If you need to add comments to configuration files, consider using JSONC (JSON with Comments) or YAML instead. Parsers that accept comments (like VS Code's JSON parser) are using a superset of the JSON standard.
What is the difference between JSON.parse() and JSON.stringify()?+
JSON.parse() converts a JSON string into a JavaScript object or value. JSON.stringify() converts a JavaScript object or value into a JSON string. Use parse when receiving JSON (e.g. from an API), and stringify when sending data or storing it as text.
Can JSON keys be numbers?+
No. In JSON, all object keys must be strings enclosed in double quotes. Numbers, booleans, and unquoted identifiers are not valid JSON keys. For example, {"1": "value"} is valid JSON, but {1: "value"} is not.
What is the maximum depth for nested JSON?+
The JSON specification does not define a maximum nesting depth, but parsers and languages often impose limits. In practice, deeply nested JSON (more than 5–10 levels) is a design smell. Flat or shallow structures are easier to query, validate, and maintain.