Skip to content

What is JSON?

Updated Nov 30, 20263 min readBased on official documentation
What is JSON?

In short

JSON stands for JavaScript Object Notation. It is a text-based data format that stores structured data as a string, so programs can send it across a network and read it back. It looks like JavaScript object syntax, but any programming language can use it.

JSON is a simple way to write structured data as plain text. Its name comes from JavaScript Object Notation, because its layout looks a lot like a JavaScript object. Even so, you do not need to use JavaScript to work with it. Most programming languages can read and write JSON.

How it works

A JSON document is a string. The string holds values in a set of nested shapes. An object groups named values inside curly braces, and each name is a key followed by a colon and a value. An array holds an ordered list of values inside square brackets. A value can be a string, a number, true, false, null, another object, or another array.

Because JSON is just text, it is easy to send over a network or save to disk. A JSON file usually ends with the extension .json, and servers label it with the MIME type application/json, which tells the receiving side what kind of content it holds.

In JavaScript, the built-in JSON object gives you two main tools. JSON.parse() turns a JSON string into a normal JavaScript object. This step is called deserialization. JSON.stringify() does the reverse. It turns an object into a string so you can send it. This step is called serialization.

A simple example

Here is a small JSON document describing a person:

{
  "name": "Molecule Man",
  "age": 29,
  "active": true,
  "powers": ["Radiation resistance", "Turning tiny"]
}

Once parsed in JavaScript, you read the data the same way you read any object. The name is available as person.name, and the second power is person.powers[1]. Arrays start counting at zero, so the first item is at index 0.

The MDN tutorial that this page is based on uses a longer example of a superhero team. It fetches the data with the Fetch API, which lets a page request resources from a server without reloading. After the data arrives, the page builds HTML elements from the parsed object. If you want to learn more about asynchronous code like this, read our page on how a race condition happens, since timing problems often appear when code waits for network responses.

Syntax rules to remember

JSON is stricter than JavaScript object literals. Some rules catch beginners often:

  • Strings must use double quotes, not single quotes.
  • Property names must be double-quoted strings.
  • Numbers must use decimal notation.
  • Objects and arrays cannot have a trailing comma after the last item.
  • Comments are not allowed.
  • Functions, undefined, NaN, and Infinity are not valid JSON.
  • Other object types such as Date, Set, and Map cannot be stored directly.

A single misplaced comma or colon makes the whole document invalid, and parsing will fail. Computer-generated JSON is less likely to contain these errors, but hand-written files often do. You can check a file with an online validator such as JSONLint before you use it.

Common mistakes

The most frequent mistake is treating a JSON string as if it were already an object. Call JSON.parse() first, then access the properties. Another mistake is forgetting that a date stored as text is still a string. If your code needs a date, convert it after parsing.

Also remember that JSON can hold data that is not wrapped in an object or array. A single number, string, or boolean is valid JSON on its own.

Alternatives

JSON is not the only data format. XML uses tags and is more verbose, and YAML is often used for configuration files because it is easier to read by hand. JSON remains popular for web APIs because browsers and most languages support it with little effort. If you plan to store large amounts of records, a database may suit you better than a file. Our guide to what a database index is explains how databases find records quickly.

For the full official reference, read the MDN JSON article.

FAQ

Is JSON only for JavaScript?

No. JSON is a text format, and most programming languages have libraries that can read and write it. JavaScript just happens to share its object syntax.

What is the difference between parsing and stringifying?

Parsing turns a JSON string into a usable object in memory. Stringifying turns an object back into a JSON string so it can be sent or saved.

Can JSON contain comments?

No. JSON does not allow comments, so any note you add will make the document invalid. Keep explanations in separate documentation instead.

Why does my JSON fail to parse?

The most common causes are a trailing comma, single quotes, or a missing comma or colon. Run the text through a validator to find the exact problem.

Sources

This page is based on the official documentation below. Read it for full detail.

JSON — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.