Skip to content

JSON

Also called: JavaScript Object Notation

Published:

What does JSON look like?

JSON is a text-based format derived from the ECMAScript language standard, with a small set of rules for representing structured data [1]. It has four primitive types (strings, numbers, booleans and null) and two structured types (objects and arrays) [1]. An object is an unordered collection of name/value pairs; names are strings, and the spec recommends keeping them unique within an object [1]. A user record might look like this: {"name": "Ada", "age": 36, "member": true, "interests": ["maths", "computing"]}.

What are the rules for writing valid JSON?

They are stricter than JavaScript object literals. Strings and property names need double quotes; single quotes are invalid. Objects and arrays cannot have trailing commas, and comments are not allowed [2]. Values such as undefined, NaN and Infinity cannot appear, nor can functions or objects like Date and Map, and numbers must be written in decimal notation [2]. JSON text exchanged between systems outside a closed ecosystem must be encoded as UTF-8 [1].

How do you read and produce JSON?

As MDN puts it, JSON is a string, which is what makes it convenient for sending data across a network [2]. In JavaScript, JSON.parse() turns a JSON string into an object and JSON.stringify() turns an object into a JSON string [2]. On the wire, JSON is labelled with the media type application/json, and files use the .json extension [1]. Ecma's ECMA-404 standard describes the same grammar; RFC 8259 notes that the two documents share it and differ only in how they describe it [1].

Frequently asked questions

Can JSON contain comments?

No. Standard JSON does not allow comments, as MDN states explicitly.

Is JSON only for JavaScript?

No. Its syntax comes from JavaScript, but RFC 8259 defines JSON as a language-independent data interchange format.

Sources

  1. RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format — IETF RFC Editor
  2. Working with JSON — MDN Web Docs