JSON Introduction
JSON stands for JavaScript Object Notation. It is a plain-text format for storing and exchanging structured data. Although its syntax was borrowed from JavaScript, JSON is completely language-independent: Python, Java, C#, Go, Rust, PHP and practically every other language can read and write it.
Here is a small JSON document describing a book:
{
"title": "The Quiet Compiler",
"pages": 312,
"inStock": true,
"tags": ["fiction", "tech"]
}Even if you have never seen JSON before, you can probably guess what every line means. That readability is a big part of why JSON became the default data format of the web.
Where is JSON used?
| Place | Example |
|---|---|
| Web APIs | A weather service answers your request with JSON |
| Configuration files | package.json, tsconfig.json, VS Code settings.json |
| Databases | MongoDB, PostgreSQL jsonb columns, CouchDB |
| Logs and events | One JSON record per line (JSON Lines) |
| Data exchange | Exporting and importing data between apps |
Why is JSON so popular?
- Simple — the whole grammar fits on a single page. There are only six kinds of values.
- Human-readable — you can open a JSON file in any text editor and understand it.
- Compact — much less markup than XML for the same data.
- Universal — every major language ships a JSON parser, usually in its standard library.
- Maps to real data structures — JSON objects become dictionaries/maps, JSON arrays become lists.
Your first JSON playground
The json playgrounds on this site validate your JSON. If the text is valid, the playground tells you what kind of value it found and prints a neatly indented copy. Press ▶ Run:
{"language": "JSON", "year": 2001, "easy": true}Valid JSON: object with 3 keys
{
"language": "JSON",
"year": 2001,
"easy": true
}
Now break it on purpose: delete one of the double quotes and run it again. You will get an Invalid JSON message explaining what went wrong. (Error messages come from the V8 engine used by Chrome and Node.js; other browsers word them differently.)
Tip: In every playground you can edit the code and press Ctrl+Enter to run it.
JSON is text
A JSON document is just a string of characters. To use the data, a program parses the text into its own data structures; to send data, it serializes (stringifies) its data structures into JSON text. In JavaScript, the two built-in functions are JSON.parse() and JSON.stringify():
const text = '{"city": "Hanoi", "population": 8500000}';
const data = JSON.parse(text); // text -> object
console.log(data.city);
console.log(data.population / 1000000, "million");
const back = JSON.stringify(data); // object -> text
console.log(back);
console.log(typeof text, typeof data, typeof back);Hanoi
8.5 million
{"city":"Hanoi","population":8500000}
string object string
You will learn both functions in detail later in this course.
A short history
JSON was popularised by Douglas Crockford in the early 2000s, who noticed that a subset of JavaScript's object literal syntax made an excellent data format. It was standardised as ECMA-404 and RFC 8259. The official file extension is .json and the media type is application/json.
| Fact | Value |
|---|---|
| Full name | JavaScript Object Notation |
| File extension | .json |
| Media (MIME) type | application/json |
| Standards | ECMA-404, RFC 8259 |
| Text encoding | UTF-8 |
A slightly bigger example
JSON values can be nested: objects can contain arrays, and arrays can contain objects. This lets you describe almost any real-world data.
{
"shop": "TAD Market",
"open": true,
"products": [
{"name": "Green Tea", "price": 4.5},
{"name": "Espresso Beans", "price": 24.5}
],
"manager": null
}Valid JSON: object with 4 keys
{
"shop": "TAD Market",
"open": true,
"products": [
{
"name": "Green Tea",
"price": 4.5
},
{
"name": "Espresso Beans",
"price": 24.5
}
],
"manager": null
}
What you will learn
This course takes you from the basic syntax to real-world topics: parsing and stringifying, nested data, common mistakes, dates and big numbers, using JSON with APIs and in Python, related formats like JSON Lines and JSON5, validating data with JSON Schema, security pitfalls, and querying JSON with jq.
Test yourself
What does JSON stand for?
- Java Standard Object Notation
- JavaScript Object Notation
- JavaScript Online Network
- Joined Serial Object Nodes
Which media type is used for JSON?
- text/json
- application/javascript
- application/json
- text/plain
Which JavaScript function turns JSON text into an object?
- JSON.stringify()
- JSON.parse()
- JSON.read()
- Object.fromJSON()