Implement Data Serialization using Node.js for JSON Data

Posted by: Mahesh Sabnis , on 12/5/2015, in Category Node.js
Views: 24953
Abstract: Node.js provides an easy to use mechanism to manage data serialization for the JavaScript object in the form of JSON

In modern web application development, data communication plays a very important role. Finally it’s all about data which is needed by the client application from the server. The hosting environment for the web application need to take proactive actions to make sure that the requested data must be serialized as per web standards. The ability of the data serialization in cross-application and cross-network scenarios in an open format holds utmost importance in web applications and the web host has to take this responsibility.


Since Node.js is based on JavaScript, we can make use of JavaScript Object Notation (JSON) and XML message format for the data communication. JSON being more close to JavaScript objects in its own way, we can easily implement data serialization from Node.js. In the application developed using Node.js, we need to decide from where we can read data and serialize it in the JSON and XML format.

Prerequisites for the implementation.

This application is developed using Visual Studio Code. This is a new FREE cross-platform editor for building and debugging modern web and cloud applications. We also need Node.js tools so that we can install necessary Node modules. Please visit following links to grab related information about Visual Studio Code.

Download Node tools from the following link

Implementing Node.js application

Step 1: Create a folder on your drive (e.g. E:\) with the name VSCodeDataSerialization. Under this folder, add a new folder of name Scripts. We will be using this to store our application files. Open Visual Studio Code. Open the VSCodeDataSerialization folder using File > Open Folder Option. Once the folder is opened, the option for creating new file will be displayed as shown in the following image. Select the Script folder and click on the new file icon as shown in the following image. This will provide a blank textbox where you create a new file name as appJSON.js.


Step 2: In appJSON.js file, we will write logic for converting JavaScript object to JSON using JSON.stringify () function. We will serialize this object over http. Add the following code in appJSON.js

var http = require('http');

var ProductInfo = [
    { ProdId: 101, ProdName: "Laptop" },
    { ProdId: 102, ProdName: "Desktop" },
    { ProdId: 103, ProdName: "Tablet" },
    { ProdId: 104, ProdName: "Mobile" },
    { ProdId: 105, ProdName: "Router" },
    { ProdId: 106, ProdName: "Printer" }

var server = http.createServer(function (req, resp) {
    resp.writeHead(200, { 'Content-Type': 'application-json' });

console.log('Server Started on 5050');


The above code performs the following operations. (Note: Comments on each line match with the numbers used in the following points.)

1. Loads the http module. Since we need to serialize data over http we need to create a web server using code.

2. Define JavaScript object which contains product information.

3. Write the response head with the Http Status code as 200 (ok) and the header content type as JSON.

4. Serialize the JavaScript object in the form of JSON using JSON.stringify () function by passing the JavaScript object to it.

5. Start accepting requests on the port 5050.

Step 3: Right-Click on the app.js and select Open in Command Prompt option. This will show the command prompt from where we can run the application.


Step 4: On the Node command prompt, add the following command

Node appJSON

This indicates a web server has started.

Step 5: Open any browser and enter the following address in the address bar:



This shows the serialized JSON data. This data can be consumed in any client side application using JavaScript frameworks e.g. Angular OR JavaScript libraries e.g. jQuery using ajax() functions.

Note: If you are using Express.js 3x the response object has a json() method which  returns the response in JSON format.

Please visit the following link to know more about the client side consumption of JSON data exposed from the Node.js

In the above code have serialized the JavaScript object into JSON. We can make use of JSON.parse () function to store the JSON data into the JavaScript object.



Node.Js provides an easy to use mechanism to manage the data serialization for the JavaScript object in the form of JSON. This leads to a lightweight data communication from Node.js to the client applications.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!