What is JSON? The Complete Guide
JSON (JavaScript Object Notation) is the language of the modern web. Learn its syntax, data types, and usage in REST APIs, and why it's the standard for data exchange.
Introduction
JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
Although derived from JavaScript, JSON is language-independent. Code for generating and parsing JSON data is available in virtually every programming language, from Python and Java to C# and Go.
Why is JSON popular?
- Simplicity: JSON has a very small grammar. It's easy to learn in minutes.
- Readability: Unlike binary formats, you can open a JSON file and understand it immediately.
- Native to JavaScript: Since the web is built on JavaScript, JSON is the natural choice for sending data between servers and browsers.
JSON Syntax and Data Types
JSON is built on two structures:
- A collection of name/value pairs (often called an object, record, struct, or dictionary).
- An ordered list of values (often called an array, list, or sequence).
Data Types
JSON supports the following data types:
- String: "Hello World"
- Number: 42, 3.14 (Integer or Floating point)
- Boolean: true, false
- Null: null
- Array: [1, 2, 3]
- Object: { "key": "value" }
Example
{
"name": "John Doe",
"age": 30,
"isDeveloper": true,
"skills": ["JavaScript", "Python", "Docker"],
"address": {
"street": "123 Tech Lane",
"city": "Silicon Valley"
},
"project": null
}Common Use Cases
REST APIs: Most modern APIs return data in JSON format.
Configuration Files: Tools like VS Code, Prettier, and many npm packages commonly use JSON for configuration (`package.json`, `.eslintrc`).
NoSQL Databases: Databases like MongoDB store data in BSON, a binary format closely related to JSON.
Tools for JSON
Working with JSON often requires tools to format, validate, or convert it.
- JSON Formatter: Prettify minified JSON to make it readable.
- JSON Validator: Check if your JSON syntax is correct.
- JSON to YAML: Convert JSON to YAML for configuration management.