Skip to main content

Command Palette

Search for a command to run...

Serialization and Deserialization in Javascript

Updated
•4 min read•View as Markdown
Serialization and Deserialization in Javascript

Serialization

Serialization is process of converting Javascript object or array into a JSON string so that it can be sent over a network (API request), stored in localStorage,files,,database and shared between different systems.

Inside JavaScript, we work with objects, but outside (like networks or storage), data must be in string format.

Serialization acts as that bridge..

These core method which is being used is JSON.stringify().

JSON.stringify()

Syntax:

JSON.stringify(value, replacer, space)

Here, value is the object you want to convert.
replacer controls what gets included or excluded . It is optional.
space which is also optional is for formatting.

Let's understand JSON.stringify() with a basic example.

const user = {
  name: "Amit",
  age: 22,
  isStudent: true
};

const jsonString = JSON.stringify(user);

console.log(jsonString);

Output:

{"name":"Amit","age":22,"isStudent":true}

Here, object gets converted to string , keys automatically wrapped in quotes and format becomes valid JSON.

When JSON.stringify() runs, JavaScript iterates over object properties, converts keys into strings, ensures values are JSON-supported types, and builds a valid JSON string.

Edge cases

  1. Functions are removed when a object is converted to string format as JSON cannot store behavior, only data.
    Example:

    const obj = {
      name: "Amit",
      greet: function() {}
    };
    
    console.log(JSON.stringify(obj));
    
    //OUTPUT
    //{"name":"Amit"}
    
  2. undefined is ignored while conversion. For example like this:

    const obj = {
      name: "Amit",
      age: undefined
    };
    
    //{"name":"Amit"}
    
  3. Date is automatically converted to string format.

    const obj = {
      createdAt: new Date()
    };
    
    console.log(JSON.stringify(obj));
    
    //{"createdAt":"2026-03-19T..."}
    
  4. Circular reference will give error as it creates infinite loop.

    const obj = {};
    obj.self = obj;
    
    JSON.stringify(obj); //Error
    

But, we can control the output of JSON.stringify() as it provides the function for it , which is replacer function.

const user = {
  name: "Alice",
  age: 22
};

const result = JSON.stringify(user, (key, value) => {
  if (key === "age") return undefined;
  return value;
});

console.log(result);

This removes the age property from the output.

We can also make the output readable by providing space value in JSON.stringify().

console.log(JSON.stringify(user, null, 2));

//Output will be like this
{
  "name": "Amit",
  "age": 22
}

Deserialization

Deserialization is converting JSON string back to original format like object so that you can access properties , run logic and manipulate data.

The core method which is used for deserialization is JSON.parse().

Syntax:

JSON.parse(text, reviver)

Here, text is JSON string and reviver which is optional modify values while parsing.

With a basic example, we can understand what JSON.parse() actually does.

const jsonString = '{"name":"Amt","age":22}';

const user = JSON.parse(jsonString);

console.log(user.name);

//Output
Amit

JavaScript reads the string and validates its JSON format. Then it converts the JSON string into object structure and JSON.parse() returns a usable object.

We need to keep in mind some edge cases which are as follows:

  1. If JSON string is invalid , so in such case JSON.parse() will throw error.
JSON.parse("{name: 'Alice'}"); //Error
  1. If you provide a random string to JSON.parse(), it will throw an error.

    JSON.parse("hello"); //Error
    

If you are receiving JSON from an external source, it’s better to safely parse it by using try catch block , means by wrapping JSON.parse() in try...catch block.

try {
  const data = JSON.parse(jsonString);
} catch (err) {
  console.log("Invalid JSON");
}

reviver function

If we want to modify the incoming data while parsing we can use built in reviver function of JSON.parse().

const user = JSON.parse(jsonString, (key, value) => {
  if (key === "age") return value + 1;
  return value;
});

In express we use app.use(express.json()) this middleware in main server.js or index.js file of backend which takes the incoming JSON string, runs JSON.parse(), and converts it into req.body.

Final Takeaway

  • JSON.stringify() → used when data is going out

  • JSON.parse() → used when data is coming in

Javascript

Part 10 of 12

This series explains core JavaScript concepts in a clear and beginner-friendly way. Each article focuses on one important topic with simple explanations, practical examples, and real development use cases. The goal is to make JavaScript easier to understand for developers who are building their foundation in web development.

Up next

Template Literals in JavaScript

Javascript template literals are denoted with backtick ( ` ) allowing for embedding dynamic expressions and content. The content is written between them. Template literals were introduced in ES6 for r