JSON Rules

Beginner
⏱️ 10 min read
📚 Updated: May 2026
Syntax & validity

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.

JSON
{
  "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": value pairs 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 (no NaN or Infinity in strict JSON).
  • Boolean — literals true or false (unquoted).
  • Null — literal null.
  • Array or object — nesting as needed.
1

Example of valid JSON

JSON
{
  "name": "Alice",
  "age": 25,
  "isEmployee": true,
  "skills": ["JavaScript", "Python", "SQL"],
  "contact": {
    "email": "alice@example.com",
    "phone": "555-1234"
  }
}
2

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 }.

Text (invalid as JSON)
{
  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).

3

Parsing JSON

JavaScript
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.

4

Stringifying JSON

JavaScript
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

1

Keys are double-quoted strings; separate members with commas; never leave a trailing comma.

2

Values are only string, number, object, array, boolean, or null—no undefined or comments in the JSON spec.

3

Use JSON.parse / JSON.stringify in JavaScript instead of eval for safe round-trips.

Frequently asked questions

No. A comma after the last property or array element makes the document invalid for JSON.parse and for strict validators.
No. Every key must be a JSON string in double quotes. Unquoted identifiers are JavaScript-only.
No. Use sidecar documentation, a "_comment" field, or a superset format with its own parser—not JSON.parse alone.
JSON is a strict interchange format as text. Object literals are JavaScript expressions with looser syntax; 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.

JSON Comment →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful