mc0de
JavaScript · Lesson 14 of 17

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.

JSON Syntax JSON.stringify() JSON.parse() Structured Data
● ● ●   data.json
{
  "name": "Alex",
  "course": "JavaScript",
  "lessons": 17,
  "active": true
}
LIVE JSON TRANSFORMER
> Select an action to explore JSON.
01 / Getting Started

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.

Example JSON document
{
  "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.

Real-world example: A learning platform could exchange JSON containing course names, lesson numbers, and completion statuses.
02 / JSON Syntax

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.

Different JSON value types
{
  "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.

Common mistake
Invalid JSON: { name: ‘Alex’, } Valid JSON: { “name”: “Alex” }
03 / JSON.stringify()

Convert JavaScript Data Into JSON

JavaScript includes a built-in method called JSON.stringify(). It converts supported JavaScript values into JSON-formatted strings.

Converting an object to JSON
const student = {
  name: "Alex",
  course: "JavaScript",
  completed: 13
};

const jsonString = JSON.stringify(student);

console.log(jsonString);
Console output
{“name”:”Alex”,”course”:”JavaScript”,”completed”:13}

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:

Pretty-printing JSON
const pretty = JSON.stringify(student, null, 2);
console.log(pretty);

The third argument specifies the indentation width. Here, 2 means two spaces.

04 / JSON.parse()

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.

Parsing a JSON string
const jsonText = '{"name":"Alex","score":95}';

const student = JSON.parse(jsonText);

console.log(student.name);
console.log(student.score);
Console output
Alex 95

After parsing, you can access object properties using dot notation or bracket notation, just as you learned in Lesson 09.

Remember: JSON.stringify() converts JavaScript data to a JSON string. JSON.parse() converts JSON text back into JavaScript data.
05 / JSON Arrays & Nested Objects

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.

A JSON array of courses
[
  {
    "title": "HTML & CSS",
    "lessons": 17
  },
  {
    "title": "JavaScript",
    "lessons": 17
  }
]

Once parsed, JavaScript can loop through the records and create HTML elements for each course.

Displaying parsed data
const jsonText = '[{"title":"HTML"},{"title":"JavaScript"}]';
const courses = JSON.parse(jsonText);

courses.forEach(function(course) {
  console.log(course.title);
});
Console output
HTML JavaScript

This connects JSON to earlier lessons on arrays, objects, loops, and DOM manipulation.

06 / Handling Invalid JSON

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.

Safe JSON parsing
const jsonText = '{"name":"Alex",}';

try {
  const data = JSON.parse(jsonText);
  console.log(data);
} catch (error) {
  console.log("Please check your JSON formatting.");
}
Console output
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.

Safety note: JSON is data, not trusted HTML. When displaying values from JSON, use textContent rather than inserting untrusted strings with innerHTML.
07 / JSON in Action

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.

Live JSON Explorer
Ready to parse your JSON.

Experiment with changing values, adding new properties, and introducing syntax errors. Observe how the parser responds.

Coming next: Lesson 15 introduces the Fetch API, where you’ll see how JavaScript retrieves JSON from web services.
🚀 mC0de Challenge

Build a JSON Data Explorer

Create an interactive webpage that accepts JSON input and transforms the data into a readable course directory.

Your Mission

  1. Create a textarea containing an array of course objects.
  2. Add a button that parses the JSON using JSON.parse().
  3. Use a loop to display each course as a card.
  4. Display the course title, lesson count, and category.
  5. Add a button that formats JSON using JSON.stringify(data, null, 2).
  6. Handle invalid JSON with try...catch.
  7. Add a Reset button to restore the original dataset.
Extra challenge: Add a search field, filter courses by category, and display the total number of lessons across all courses.
💻 Interactive JavaScript Sandbox

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.

Live Result
Lesson 14 Recap

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 Completed

Lesson 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!