Skip to content
Concept 29 // Module 03: Data Structures & Programming Fundamentals

JSON: Serialization, Parsing & JSON.rawJSON()

Module 03: Data Structures & Programming Fundamentals•Concept 29 of 100• 5 min read

JSON: Serialization, Parsing & JSON.rawJSON()

**JSON** (JavaScript Object Notation) is a lightweight data-interchange format. It is a text-based, language-independent format that is easy for humans to read/write and easy for machines to parse/generate.

The Core Concept

Syntax Reference
// 1. Serialization (Object to String)
JSON.stringify(obj);

// 2. Deserialization (String to Object)
JSON.parse(jsonString);

// 3. With Formatting (Pretty-print)
JSON.stringify(obj, null, 2);

Live Interactive Studio

`JSON.stringify` converts a JS object into a string for transport (like an API call). `JSON.parse` converts it back into a usable object.
JavaScript Editor
Loading...
Console Output
Waiting for output...

Key Code Patterns in Action

01

Basic Parse & Stringify

// 1. Basic Parse & Stringify
const user = { id: 1, name: "Subhajit" };

const jsonString = JSON.stringify(user);
console.log(typeof jsonString); // "string"

const backToObj = JSON.parse(jsonString);
console.log(backToObj.name); // "Subhajit"
`JSON.stringify` converts a JS object into a string for transport (like an API call). `JSON.parse` converts it back into a usable object.
02

The Replacer & Reviver

// 2. The Replacer & Reviver
const data = { date: new Date(), secret: "password" };

const filtered = JSON.stringify(data, (key, value) => {
  return key === "secret" ? undefined : value;
});

console.log(filtered); // "{\"date\":\"2025-12-17T...\"}"
You can pass a function to `stringify` to filter out properties (like passwords) or format data during serialization.
03

Deep Cloning Hack

// 3. Deep Cloning Hack
const original = { a: 1, b: { c: 2 } };
const clone = JSON.parse(JSON.stringify(original));

clone.b.c = 99;
console.log(original.b.c); // 2 (Original is safe!)
While modern JS has `structuredClone()`, stringifying and parsing is a classic hack to deep-copy an object without maintaining references.
04

1. Core Usage: JSON

// Core syntax and usage for JSON: Serialization, Parsing & JSON.rawJSON()
const feature = 'json-serialization-parsing-rawjson';
console.log('Concept 29:', feature);
console.log('Active:', true);
Illustrates the fundamental invocation and syntax structure for JSON: Serialization, Parsing & JSON.rawJSON().
05

2. Defensive Handling & Edge Case

// Edge-case defensive handling
function handleSafe(input) {
  if (input === null || input === undefined) return 'safe-fallback';
  return String(input).trim();
}
console.log(handleSafe('json-serialization-parsing-rawjson'));
console.log(handleSafe(null));
Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with JSON: Serialization, Parsing & JSON.rawJSON().
06

3. Clean Architecture & Production Pattern

// Production-grade pattern for JSON: Serialization, Parsing & JSON.rawJSON()
const settings = Object.freeze({
  id: 'json-serialization-parsing-rawjson',
  ready: true,
  timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);
Demonstrates clean, immutable production code avoiding unintended state mutations.

Real-World Engineering Takeaways

APIs: Sending and receiving data from web servers.
Storage: Saving settings or state in `localStorage`.
Config: Managing app settings in `.json` files.
Logging: Sending structured event data to monitoring tools.

Under the Hood: Engine & Memory Mechanics

How JavaScript Executes This
### **Parsing & Allocation** **1. String to Object:** - When `JSON.parse()` runs, the engine scans the string and builds a corresponding **Object Tree** in the **Heap**. - This can be memory-intensive for large files. **2. Serialization:** - `JSON.stringify()` "walks" the object tree in memory and converts it into a **UTF-16 string** in the **RAM**. **3. Performance:** - JSON parsing is a "blocking" operation on the main thread. - **Rule:** For extremely large JSON objects (MBs), consider streaming the data or parsing it in a **Web Worker** to avoid freezing the **CPU** and UI.

Quick Knowledge Check

Test your understanding on these 10 questions (one by one):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

What is the core purpose of JSON: Serialization, Parsing & JSON.rawJSON() in JavaScript?

// Concept 29: JSON: Serialization, Parsing & JSON.rawJSON()
// Understanding the core mental model
1 / 10