JavaScript · Lesson 24 / 32 · English
JSON
Understand the idea. Change the code. Build confidence with one focused experiment.
Read this lesson in தமிழ் ↗.
Start with the idea
JSON is a text format for exchanging data. stringify converts a supported JavaScript value into JSON text; parse reads JSON text back into a value.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
Step-by-step lesson notes
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write, and easy for machines to parse. JSON is the standard format for web APIs.
JSON Syntax
{
"name": "Alice",
"age": 30,
"isAdmin": false,
"address": {
"city": "New York",
"zip": "10001"
},
"hobbies": ["reading", "coding", "hiking"],
"spouse": null
}| JSON Type | Example | Notes |
|---|---|---|
| String | "hello" | Must use double quotes |
| Number | 42, 3.14 | No NaN or Infinity |
| Boolean | true, false | Lowercase |
| Null | null | Lowercase |
| Array | [1, 2, 3] | Ordered list |
| Object | {"key": "val"} | Unordered key-value pairs |
JSON.stringify and JSON.parse
const user = { name: "Alice", age: 30, active: true };
// JavaScript object → JSON string
const json = JSON.stringify(user);
console.log(json); // '{"name":"Alice","age":30,"active":true}'
// Pretty print
const pretty = JSON.stringify(user, null, 2);
console.log(pretty);
// {
// "name": "Alice",
// "age": 30,
// "active": true
// }
// JSON string → JavaScript object
const parsed = JSON.parse(json);
console.log(parsed.name); // "Alice"
// With a reviver function
const dateJson = '{"date": "2024-01-15"}';
const obj = JSON.parse(dateJson, (key, value) => {
if (key === "date") return new Date(value);
return value;
});
console.log(obj.date instanceof Date); // trueJSON limitations: Functions, undefined, Symbol, and circular references are not supported in JSON and will be dropped or cause an error during stringification.
A complete example
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
const text = '{"title":"JavaScript","lessons":32}';
const course = JSON.parse(text);
console.log(course.title);
console.log(JSON.stringify({ lessons: course.lessons + 1 }));Predict first. Reveal the expected result.
JavaScript
{"lessons":33}Make one change. See what happens.
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Wait for the editor to load, then load this example and press Run.
Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.
Put the idea to work
Practice challenge
Add a boolean published property before serializing the result.
Reveal the answer and reasoning
JSON.stringify({lessons: 33, published: true}) produces JSON containing both properties.
One mistake to avoid: JSON property names require double quotes. JSON cannot contain comments, functions, or trailing commas.
Your learning checkpoint
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.