• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking
○Declaring Variables○let and const in Practice○Operators○Type Conversion
○Conditionals○Switch Statement○For Loops○While Loop○Break and Continue
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions
○Arrays○Array Mutation Methods○Array Search and Slice○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every
○Objects○Object Methods and Destructuring●JSON○Math and Date○Date Basics
○02 Template Literals○String Methods○Regular Expressions
○Higher-Order Functions○Callbacks○map, filter, and reduce○Recursion○Project - Data Transform Pipeline
○OOP Introduction○Classes○Inheritance○Encapsulation
○Asynchronous JavaScript○Promises and Async Await○Async API Simulation

JSON

JSON (JavaScript Object Notation) is the standard format for exchanging data between systems — APIs, config files, local storage. It looks like a JavaScript object literal but with stricter rules.

JSON rules

  • All keys must be quoted with double quotes.
  • Values can be: string, number, boolean, null, array, or object.
  • No functions, undefined, or comments.
json
{
    "name": "Alice",
    "age": 30,
    "skills": ["JavaScript", "HTML"],
    "address": { "city": "London" }
}

JSON.stringify() — object → string

javascript
const user = { name: "Alice", age: 30 };
const json = JSON.stringify(user);
console.log(json);            // '{"name":"Alice","age":30}'
console.log(typeof json);     // "string"

// Pretty-print with 2-space indent
JSON.stringify(user, null, 2);

JSON.parse() — string → object

javascript
const data = '{"name":"Bob","score":95}';
const obj = JSON.parse(data);
console.log(obj.name);  // "Bob"
console.log(obj.score); // 95

Your Task

  1. Given the object user = { name: "Sam", level: 5, active: true }, serialize it to a JSON string and store in userJson.
  2. Parse the string apiResponse = '{"status":"ok","count":42}' and store the result in parsed.
  3. Write a function deepClone(obj) that uses JSON serialization to create a deep copy of an object.
  4. Display in #output: "Sam is level 5 | status: ok, count: 42".
Hint 1
1 / 3
HINT 1

Use JSON.stringify(obj) to convert an object to a JSON string.

Loading editor…
READY
intercourses
javascript