JSON (JavaScript Object Notation)
JSON is a lightweight text format used to exchange structured data between web pages, APIs, and many programming languages.
What you will learn
- How JSON objects and arrays are written
- How JSON differs from a JavaScript object
- How to display API data safely
Basic JSON structures
{
"name": "Aki",
"tags": ["web", "beginner"],
"active": true
}JSON uses double-quoted property names and values such as strings, numbers, booleans, arrays, objects, or null. It does not contain JavaScript comments or functions.
Using JSON in JavaScript
const text = '{"name":"Aki"}';
const data = JSON.parse(text);
console.log(data.name);
const output = JSON.stringify(data);JSON.parse() converts JSON text into a JavaScript value. JSON.stringify() converts a JavaScript value into JSON text for storage or transmission.
Safety points
- Validate data received from an API before relying on its shape.
- Use
textContentwhen displaying untrusted values instead of inserting them as HTML. - JSON is data, not executable code; do not evaluate it with
eval().