JSON Rules

What you’ll learn
JSON is the default wire format for most web APIs and many config files. If a document breaks even one rule, JSON.parse in the browser (and strict pipelines) will throw SyntaxError.
This page collects the structural rules for objects, arrays, and values, shows a valid document beside common mistakes, and ends with a short JavaScript refresher for parse and stringify.
What is JSON?
JSON (JavaScript Object Notation) is a text format for structured data. It is language-independent but borrows familiar brace and bracket syntax from the C family of languages. Parsers expect an exact grammar; it is not the same as a free-form JavaScript object literal.
Why JSON is popular
Lightweight
Fewer tokens than XML for the same data, which keeps payloads smaller on the network.
Readable
Nested objects and arrays map cleanly to how developers think about records and lists.
Interoperable
Every mainstream stack can read and write JSON; schemas and OpenAPI build on top of it.
Structured
Arbitrary depth of nesting makes it suitable for APIs, logs, and document stores.
Basic structure
A JSON object is an unordered map of string keys to values. A JSON array is an ordered list of values. The top-level of a file or message is usually an object or an array.
{
"name": "John Doe",
"age": 30,
"isStudent": false,
"courses": ["Math", "Science", "History"],
"address": {
"street": "123 Main St",
"city": "Anytown",
"zipcode": "12345"
}
}- Object — wrapped in
{ }; members are"key": valuepairs separated by commas. - Array — wrapped in
[ ]; elements are separated by commas. - Key/value — keys are always JSON strings (double quotes). Values use one of the six JSON types.
JSON syntax rules
These rules mirror RFC 8259 and what JSON.parse enforces in JavaScript.
Objects
- Wrapped in curly braces
{ }. - Members are separated by commas; each member is a string key, a colon, and a value.
- Keys must be JSON strings in double quotes.
Arrays
- Wrapped in square brackets
[ ]. - Elements are comma-separated values (any JSON type, including objects and arrays).
Values
Each value must be one of:
- String — Unicode text in double quotes, with escapes where needed.
- Number — decimal digits; optional
-, fraction, and exponent (noNaNorInfinityin strict JSON). - Boolean — literals
trueorfalse(unquoted). - Null — literal
null. - Array or object — nesting as needed.
Example of valid JSON
{
"name": "Alice",
"age": 25,
"isEmployee": true,
"skills": ["JavaScript", "Python", "SQL"],
"contact": {
"email": "alice@example.com",
"phone": "555-1234"
}
}Common mistakes (not valid JSON)
The snippet below mixes patterns that are easy to write by hand but fail strict JSON: unquoted keys, a missing comma between members, // comments, and a trailing comma before }.
{
name: "Alice",
"age": 25,
// comments are not allowed in JSON
"isEmployee": true
"skills": ["JavaScript", "Python", "SQL"],
"contact": {
"email": "alice@example.com",
"phone": "555-1234",
}
}Shown as plain text so it is not mistaken for a parseable JSON document. Fix keys, commas, comments, and trailing commas before sending to an API or running JSON.parse.
CI and browsers agree
What loads in an editor with a JSONC plugin may still fail in production when the same bytes hit JSON.parse, a strict schema validator, or another language’s parser. Treat “green in the IDE” as a hint, not proof of standards-compliant JSON.
Working with JSON in JavaScript
The language has two built-ins: JSON.parse for text → values, and JSON.stringify for values → text. See the dedicated tutorials for edge cases (reviver, replacer, pretty printing).
Parsing JSON
const jsonString = '{"name": "Alice", "age": 25}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "Alice"JSON.parse() guide covers syntax errors and the optional reviver.
Stringifying JSON
const obj = { name: "Alice", age: 25 };
const jsonString = JSON.stringify(obj);
console.log(jsonString); // '{"name":"Alice","age":25}'Object literals in code allow unquoted keys; the output string from JSON.stringify always uses JSON rules. Continue on the JSON.stringify() page for formatting options.
Typical use cases
Strict JSON is the default whenever text must parse the same way in browsers, servers, and tooling. These are the usual places that assumption shows up.
- HTTP APIs
- Request and response bodies in REST and many GraphQL transports.
- Configuration
- App and build settings (watch for comment-friendly supersets vs strict JSON).
- Storage
- Document databases and JSON columns in SQL engines.
- Interchange
- Queues, event logs, and CLI tools passing structured messages as text.
Key takeaways
Keys are double-quoted strings; separate members with commas; never leave a trailing comma.
Values are only string, number, object, array, boolean, or null—no undefined or comments in the JSON spec.
Use JSON.parse / JSON.stringify in JavaScript instead of eval for safe round-trips.
Frequently asked questions
JSON.parse and for strict validators."_comment" field, or a superset format with its own parser—not JSON.parse alone.JSON.stringify converts values into compliant JSON text.Next: JSON Comment
Learn why the JSON spec omits comments and which patterns teams use in real configs and APIs.
Strict JSON (for example RFC 8259) is a subset of what you can write in JavaScript object literals: no single-quoted strings, no trailing commas, no undefined, and no // or /* */ comments. Tools that accept “JSON with comments” are using a different grammar.
10 people found this page helpful
