Understanding JSON
Learn how websites organize, store, and exchange structured data. Convert JavaScript objects into JSON strings, turn JSON back into objects, and display information dynamically.
{
"name": "Alex",
"course": "JavaScript",
"lessons": 17,
"active": true
}
What Is JSON?
JSON stands for JavaScript Object Notation. It is a text-based format for representing structured data, such as student profiles, product details, or lists of courses.
JSON looks similar to a JavaScript object, but JSON is a data format—not executable JavaScript code.
{
"name": "Alex",
"course": "JavaScript",
"lessonsCompleted": 13,
"active": true
}Each property has a key and a value. In JSON, object keys must use double quotation marks.
Understand the Rules of JSON
JSON follows a few strict rules. Objects use curly braces, arrays use square brackets, and object property names must be double-quoted strings.
JSON supports strings, numbers, booleans, null, objects, and arrays.
{
"title": "Learn JavaScript",
"lessons": 17,
"published": true,
"rating": null,
"topics": ["HTML", "CSS", "JavaScript"],
"author": {
"name": "Alex",
"role": "Instructor"
}
}Unlike JavaScript object literals, JSON does not allow comments, functions, undefined, single-quoted strings, or trailing commas.
Convert JavaScript Data Into JSON
JavaScript includes a built-in method called JSON.stringify(). It converts supported JavaScript values into JSON-formatted strings.
const student = {
name: "Alex",
course: "JavaScript",
completed: 13
};
const jsonString = JSON.stringify(student);
console.log(jsonString);The result is a string, not an object. It can be transmitted to an API or stored as text.
For readable formatting, add two more arguments:
const pretty = JSON.stringify(student, null, 2);
console.log(pretty);The third argument specifies the indentation width. Here, 2 means two spaces.
Turn JSON Strings Back Into Data
When JavaScript receives a JSON string, it can use JSON.parse() to convert that text into a JavaScript value.
const jsonText = '{"name":"Alex","score":95}';
const student = JSON.parse(jsonText);
console.log(student.name);
console.log(student.score);After parsing, you can access object properties using dot notation or bracket notation, just as you learned in Lesson 09.
JSON.stringify() converts JavaScript data to a JSON string. JSON.parse() converts JSON text back into JavaScript data.Work With Collections of Information
Real applications often contain lists of records rather than just one object. JSON can represent arrays of objects, which are especially useful for displaying cards, tables, and directories.
[
{
"title": "HTML & CSS",
"lessons": 17
},
{
"title": "JavaScript",
"lessons": 17
}
]Once parsed, JavaScript can loop through the records and create HTML elements for each course.
const jsonText = '[{"title":"HTML"},{"title":"JavaScript"}]';
const courses = JSON.parse(jsonText);
courses.forEach(function(course) {
console.log(course.title);
});This connects JSON to earlier lessons on arrays, objects, loops, and DOM manipulation.
Prevent Parsing Errors From Breaking Your App
Not every JSON string is valid. Missing quotation marks, extra commas, or incomplete braces can cause JSON.parse() to throw a SyntaxError.
Use try...catch to handle invalid JSON gracefully.
const jsonText = '{"name":"Alex",}';
try {
const data = JSON.parse(jsonText);
console.log(data);
} catch (error) {
console.log("Please check your JSON formatting.");
}Instead of crashing the interaction, your program can display a helpful message and allow the visitor to correct their JSON.
textContent rather than inserting untrusted strings with innerHTML.Try the Live JSON Parser
Modify the JSON below, then select Parse JSON. The explorer will validate the syntax and display information from your data.
Experiment with changing values, adding new properties, and introducing syntax errors. Observe how the parser responds.
Build a JSON Data Explorer
Create an interactive webpage that accepts JSON input and transforms the data into a readable course directory.
Your Mission
- Create a textarea containing an array of course objects.
- Add a button that parses the JSON using
JSON.parse(). - Use a loop to display each course as a card.
- Display the course title, lesson count, and category.
- Add a button that formats JSON using
JSON.stringify(data, null, 2). - Handle invalid JSON with
try...catch. - Add a Reset button to restore the original dataset.
Build Your JSON Data Explorer!
The starter project parses JSON and creates course cards. Select Load Solution for search, category filtering, JSON formatting, and a live lesson-count summary.
What You’ve Learned
You’re Ready to Work With Structured Data!
You’ve learned how JSON represents information and how JavaScript can convert, parse, and display that information.
- Understand JSON syntax and supported data types.
- Convert objects into JSON strings with
JSON.stringify(). - Convert JSON strings into data with
JSON.parse(). - Work with arrays and nested objects.
- Handle invalid JSON using
try...catch. - Display JSON data safely using DOM methods.
- Build a JSON Data Explorer.
Next up: Fetch API — learn how to request and display information from external data sources!
Your Learning Journey
0 / 17 CompletedLesson 14 of 17 — just three more lessons after this one!
You've Mastered JSON Basics!
You can now create JSON data, convert JavaScript objects into JSON strings, parse structured information, and display dynamic content on webpages. Your JSON Data Explorer is ready!


