What is JSON? Learn all about JSON in 5 minutes

Apr 14, 2023 | Specification

What is JSON? It’s a lightweight data interchange format. JSON is for example used to transfer data on the web between browsers, mobile devices, and servers. It is also used as data format in databases like MongoDB and CouchDB. And in the JavaScript ecosystem it is used for configuration files.

This article answers the question “What is JSON” and learns you all the details.

What is a data format?

A data format is a way to describe the structure of data. For example, if I send you the following text:

Joe,Sarah,Robert

And I tell you that it is a list with comma separated names, you immediately understand the structure of the text and you see that it contains three names (Joe, Sarah, and Robert). In this case, the data format is a comma separated list with values. JSON is similar to that, but it is more extensive than our example above.

The JSON data format

JSON was discovered by Douglas Crockford as a subset of JavaScript. JSON stands for JavaScript Object Notation, and you can find the official definition at https://www.json.org

Here is a typical example of a JSON document:

[
  {
    "name": "Joe",
    "nickname": "Big Joe",
    "score": 12.4,
    "subscribed": true
  },
  {
    "name": "Sarah",
    "nickname": "Sadie",
    "score": 9.7,
    "subscribed": false
  },
  {
    "name": "Robert",
    "nickname": null,
    "score": 9.2,
    "subscribed": false
  }
]

This is a list containing three objects. Each object has four keys (name, nickname, score, and subscribed) and corresponding values. The example shows all different types of values that JSON has: array, object, string, number, boolean, and null. These types will be explained in detail in the following sections. 

If you are familiar with TypeScript, the following type definition nicely summarizes the JSON format:

type JSONValue 
  = { [key: string]: JSONValue } // object
  | JSONValue[]                  // array
  | string 
  | number 
  | boolean
  | null

Array

An array is an ordered list with values. An array is enclosed in square brackets [...], and contains values separated by a comma.

[ value1, value2, value3, ... ]

An array value can be anything: a nested array, nested object, string, number, boolean, or null. An array can contain zero, one, or multiple values.

Object

An object is an unordered set with key/value pairs. An object is enclosed in braces {...}. Key/value pairs are separated by a comma. A key and a value are separated by a colon. The key is a string enclosed in double quotes. The key is sometimes called “name” or “property”.

{ "key1": value1, "key2": value2, "key3": value3, ... }

Just like with array values, object values can be anything: a nested array, nested object, string, number, boolean, or null. An object can contain zero, one, or multiple key/value pairs.

String

A string is a sequence of characters enclosed in double quotes. JSON supports unicode characters. Example:

"Hello world!"

Special characters have to be escaped with a backslash. For example, a double quote can be inserted in a string like:

"This text contains an escaped double quote \" and a new line \n, you see?"
JSON has the following escape characters:
  • \" double quote
  • \\ backslash
  • \/ forward slash (needed when embedding in PHP or a script tag, since you can have </ otherwise, see discussion)
  • \b backspace
  • \f form feed
  • \n line feed
  • \r carriage return
  • \t tab
  • \u followed by four hex values, for example which is a phone

Number

A number can be an integer or floating point number, and can have optional exponent. Some examples:

42
-123
12.5
3.1e3
3.1E3
3.1e+3
0.1e-12
-0.33E9

Boolean

There are two boolean values supported: true and false. They are written in lowercase:

true
false

Null

The value null (lowercase) can be used to denote the absence of a value, like the value of nickname in the following example:

{
  "name": "Robert",
  "nickname": null,
  "score": 9.2,
  "subscribed": false
}

Whitespace

You may have noticed that in the examples above the data is nicely formatted and indented. In JSON, you can add optional whitespace around keys and values. Formatting a JSON document makes it easier to read, but makes the document larger due to the added white space characters. When sending a JSON document or storing it, it is often best to compact the document (remove whitespacing) to minimize the amount of data that needs to be stored or transferred. You can read more about this in the article “Beautify JSON: 3 easy ways”.

The following characters are valid white space characters:

  • Space
  • Linefeed \n
  • Carriage return \r
  • Tab \t

Encoding

The JSON specification requires UTF-8 encoding. You normally don’t have to worry about that, this is handled by the JSON library that you use.

JSON limitations and alternatives

Since the JSON data format is so simple, it is also limited. Known critiques are the lack of support for comments (useful when using JSON for configuration files), and the lack of support for more advanced data structures like Dates, and the data format being verbose for example due to the required double quotes around all keys and strings and also because the format is a text based format.

Note that JSON is flexible enough to overcome these limitations with conventions. You can add comments as a regular key/value pair like {"comment":"only enable this config in production"}. And you can serialize a date in a string for example, like {"updated":"2022-10-31T09:00:00Z"}.

There are JSON variants that may offer a better alternative than JSON in specific cases. For example:

  • JSON5 is a data format better suitable for configuration files than JSON. it is a superset of JSON. It for example makes double quotes around keys optional and supports comments.
  • BSON is a binary data format. It originates from the MongoDB database, and was designed specifically to store JSON data in a more compact and efficient way.

Frequently asked questions

How can I use JSON data?

JSON data is normally used inside applications, like a JavaScript browser application and a backend server application. Most programming languages have support for JSON, allowing you to parse JSON data into an object and stringify it again. You can read more about that in the article “Parse JSON: What is JSON parsing and how does it work?”.

Why doesn’t JSON support comments?

Basically, the reason was to keep JSON simple and prevent misuse of comments for all kinds of metadata like parsing directions. You can read more about this in the article “Why JSON comments are not allowed and what to do about it”.

Why doesn’t JSON support dates and other more advanced data types?

JSON was really designed to be as simple as possible, and it does not have support for dates for example. You can define your own conventions though to serialize dates or other advanced data types, JSON is flexible enough for that. You can read more about that in the article “JSON date format: 3 ways to work with dates in JSON”.

Why is JSON so popular?

The JSON data format is so simple and yet so powerful. It is supported in all programming languages, and it is a plain text format making it easy to work with. As Douglas Crockford explains in this talk about JSON (at 11:08):

“JSON is the intersection of modern programming languages. […] Prior, data interchange formats tended to try to be the union of all languages and that turns out to be horrendously complex and really difficult to deal with. JSON, by being so simple, actually became really easy to use.”

Is JSON a programming language?

No. JSON is a data format. JSON is a subset of JavaScript though, which is a programming language.

Is it difficult to learn JSON?

No! You can learn JSON in 5 minutes by reading this article :).

Is a JSON file a database?

No, a JSON file is not a database. If you have a JSON file and you want to search, sort, query, filter, or transform it, you can use a tool such as JSON Editor Online. The article “Easily transform JSON data: a hands-on tutorial” explains how to do that. Also, there are databases though such as MongoDB that store JSON documents, index them, and make them queryable.

What are JSON files?

A JSON file is a regular plain text file containing a JSON document. Normally, these files have the extension .json. You can open such a file with any plain text editor, or open it in JSON Editor Online.

What is the difference between JSON and XML?

Both JSON and XML are data interchange formats. JSON is a much simpler and more compact format. In practice, JSON is almost always the better choice. You may choose XML over JSON when dealing with a legacy system that only supports XML, or when you have very specific constraints.