JSON (JavaScript Object Notation) is an open-standard file format that uses human-readable text to transmit objects consisting of key-value pairs and array data types. While its name references JavaScript, JSON is a language-independent format that has become a staple in modern web development.
JSON is a data format, much like HTML is a format for hypertext documents or DOCX for Word documents. JSON doesn’t “run” like JavaScript; it’s simply a string that represents data in a structured format. It’s designed to be easily read and understood by both humans and machines.
JSON is just a string. It’s just text.
This simplicity is the key to JSON's widespread adoption. However, its resemblance to JavaScript syntax sometimes causes confusion.
JSON was introduced as a lightweight alternative to XML for data interchange. XML, though powerful, was verbose and difficult to read for humans. JSON’s syntax, inspired by JavaScript object literals, offered a simpler and more efficient solution.
A literal in JavaScript is a value that you type directly into your code. For example:
// JavaScript literals
true // Boolean
3.14 // Number
"Hello" // String
[1, 2, 3] // Array
{ key: "value" } // Object
JSON borrowed this concept, adapting it for data representation.
JSON represents these values in a straightforward way:
| JavaScript Value | JSON Representation |
|---|---|
| true | "true" |
| 12.34 | "12.34" |
| null | "null" |
JSON always uses double quotes for strings, and any internal quotes must be escaped with a backslash:
"Bob said, \"Hello!\"" // Correct JSON string
JSON arrays look just like JavaScript arrays:
[1, 2, 3] // JSON array
In JSON, all keys must be enclosed in double quotes:
{
"person": true,
"name": "Roberta"
}
Serialization is the process of converting data into a string format (like JSON) for transmission or storage. Deserialization is the reverse process: converting a JSON string back into a usable data structure.
JavaScript makes this easy with its built-in JSON object:
// Serialize a JavaScript object to JSON
const obj = { key: "value" };
const jsonString = JSON.stringify(obj);
// Deserialize JSON back to a JavaScript object
const parsedObj = JSON.parse(jsonString);
const data = {
name: "Alice",
age: 30,
hobbies: ["reading", "cycling"]
};
// Serialize to JSON
const jsonData = JSON.stringify(data);
console.log(jsonData);
// Output: '{"name":"Alice","age":30,"hobbies":["reading","cycling"]}'
// Deserialize back to JavaScript object
const parsedData = JSON.parse(jsonData);
console.log(parsedData.name); // Output: Alice
JSON is commonly used to exchange data between a client (e.g., a web browser) and a server. Here's an example of how a JSON API might respond to a request for user data:
// Server Response (JSON)
{
"id": 1,
"name": "Alice",
"email": "alice@example.com"
}
// JavaScript Code
fetch("https://api.example.com/user/1")
.then(response => response.json())
.then(data => console.log(data.name)); // Output: Alice
JSON.stringify and JSON.parse for working with JSON.With this foundational knowledge, you're ready to explore how JSON powers modern web development, from APIs to configuration files.