JSON, short for JavaScript Object Notation, is the most widely used data interchange format on the modern web. Every time you refresh a social feed, check the weather in an app, or submit a form on a website, there is a very good chance JSON is carrying that data between the client and the server. In this opening lesson of our JSON tutorial series, you will learn what JSON is, why it became the default language of APIs, and what a real JSON document looks like.
JSON is a lightweight, text-based format for representing structured data. It was derived from JavaScript object literal syntax by Douglas Crockford in the early 2000s, but it is completely language independent. Practically every programming language, including Python, Java, C#, Go, Ruby, PHP, and Rust, ships with built-in or standard-library support for reading and writing JSON.
A JSON document represents data as name and value pairs and ordered lists. Here is a small but realistic example describing a user account:
{
"id": 1042,
"username": "ada_lovelace",
"email": "ada@example.com",
"isActive": true,
"roles": ["admin", "editor"],
"profile": {
"fullName": "Ada Lovelace",
"country": "UK"
}
}Even without any training, you can probably read this document and understand it. That human readability is one of the main reasons JSON won the format wars.
Before JSON, most systems exchanged data using XML, which is verbose and requires heavyweight parsers. JSON offered several practical advantages:
JSON.parse, and every language has a fast parser.These qualities made JSON the default body format for REST APIs, configuration files such as package.json, NoSQL databases like MongoDB, and logging pipelines.
JSON is built from just two structures:
{ }.[ ].Values can be strings, numbers, booleans, null, objects, or arrays. Because objects and arrays can nest inside each other, these two simple structures can describe almost any data you can imagine, from a shopping cart to an entire application state.
Open your browser console and run the sample code attached to this lesson. It converts a JavaScript object into a JSON string and back again, which is the round trip you will perform constantly as a developer.
Next up: JSON Syntax Rules, where we cover the exact grammar that makes a document valid JSON.