Guides

What Is JSON?

The text format behind most modern APIs—strict syntax, small vocabulary, big ecosystem.

Updated April 10, 2026

What JSON is

JSON (JavaScript Object Notation) is a lightweight, language-independent data interchange format. Douglas Crockford popularized it for browsers and servers to exchange structured information without proprietary binary protocols. Today it is the default payload for REST and many GraphQL transports, configuration snapshots, and logs.

JSON encodes hierarchical data as UTF-8 text. It is easy for humans to read when pretty-printed and straightforward for machines to parse with predictable performance. Its grammar is intentionally small so implementations stay compatible across ecosystems.

Syntax rules

JSON builds documents from two structures: objects and arrays. An object is an unordered set of "key": value pairs wrapped in braces. Keys must be strings in double quotes. An array is an ordered list of values inside square brackets, separated by commas.

Whitespace between tokens is allowed and ignored by parsers. Numbers, strings, booleans, and null appear as values. You cannot leave a trailing comma after the last element—strict parsers reject that immediately. Strings use double quotes only; single-quoted strings belong to JavaScript literals, not JSON.

Data types

JSON defines six value kinds:

  • String: Unicode text escaped with backslashes for quotes, slashes, control characters, and \uXXXX escapes.
  • Number: Decimal notation with optional exponent; no separate integer type. IEEE 754 doubles are the usual runtime representation—watch precision for large integers.
  • Boolean: lowercase true or false.
  • Null: the literal null for absence of value.
  • Object: string-keyed map of nested values.
  • Array: ordered sequence of any JSON values, including mixed types if you allow that in your schema.

JSON vs XML

XML wraps data in tagged trees with attributes and mixed content. It excels when you need namespaces, document order, or schema systems deeply integrated with enterprise tooling. JSON tends to be more readable for simple records and smaller on the wire when payloads are object-heavy—fewer closing tags, less ceremony.

Parsing is typically faster in web stacks because JSON maps cleanly to native objects and arrays, while XML demands DOM or streaming APIs and often more memory. Choose XML when standards require it (SOAP, certain feeds); choose JSON for most greenfield HTTP APIs and browser-first workflows.

JSON in APIs

HTTP APIs send JSON in POST, PUT, and PATCH bodies and return it from GET endpoints. Clients set Content-Type: application/json and parse responses with the same assumption. Versioned fields and explicit nulls help evolve schemas without breaking older clients—optional keys can disappear, but changing a type breaks consumers.

Error objects often mirror problem details proposals: machine-readable code, human message, and optional details arrays. Logging middleware frequently records JSON lines for structured search in observability tools.

JSON in config files

Tools like npm, VS Code settings, and cloud deployment templates use JSON (or supersets) for configuration. Benefits: diff-friendly, easy to generate from scripts, universal parsers. Drawbacks: no comments in strict JSON, and large files can be hard for humans to edit without formatting help.

Teams sometimes generate JSON from YAML or TypeScript during build steps to keep author ergonomics while shipping standards-compliant JSON to production. Validate generated files in CI so a typo does not reach deploy.

Common mistakes

New authors often hit the same walls. Trailing commas after the final property or array element invalidate the document. Single quotes around strings fail strict parsers. Comments—even brief // notes—are not JSON unless you adopt an extended dialect and configure tooling accordingly.

Unquoted keys, undefined values, NaN, Infinity, and functions cannot appear in JSON output from JSON.stringify—they become null or drop, depending on the case. Always test edge cases: empty strings, zero, empty objects, and deeply nested structures that might hit stack limits in naive parsers.

Parsing JSON in JavaScript

Use JSON.parse(text) to turn JSON strings into plain JavaScript values. Wrap calls in try/catch when handling user input—syntax errors throw exceptions. JSON.stringify(value, replacer, space) serializes values; the optional space argument pretty-prints for debugging.

Reviver and replacer callbacks filter or transform values during parse and stringify, handy for dates or bigint experiments (note bigint still lacks native JSON support). In browsers, avoid eval on JSON-like text; it opens code injection risks.

JSON Schema for validation

JSON Schema describes allowed shapes: required properties, string formats (email, URI), numeric ranges, enums, and nested objects. Validators report precise paths like /items/3/price when data fails checks. Schemas can live beside OpenAPI documents to keep documentation and enforcement aligned.

Start with draft versions supported by your toolchain, pin versions in CI, and test both positive and negative fixtures. Schema evolution is easier when you add optional fields instead of repurposing types clients already rely on.

Frequently asked questions

Can JSON have comments?
Not in standard JSON. Use JSONC/JSON5-aware tools if you need comments, or store documentation in separate fields.
What is the difference between JSON and a JavaScript object?
JSON is serialized text with strict quoting rules; objects are live values with optional prototypes and methods. Parse and stringify bridge the two.
How do I validate JSON?
Parse first, then apply JSON Schema or your API contract tests. Automate checks in CI for examples and configs.
Is JSON a programming language?
No—it is a data format without control flow or execution semantics.

Related guides

Related tools