How to Convert String to Json using Node Js?

Jan 29, 2022 . Admin

Hello Friends,

This article will give you example of convert query string to json node js. I explained simply about convert a string to json using node js. This tutorial will give you simple example of how to convert a string to json in node js. This is a short guide on example to convert a string to json nodejs

In this post, You'll learn how to convert string into json object in node js. i will show you convert string to json node js.We will use convert string to json in nodejs.

Here i will give you many example how to convert query string to json nodejs.

So, let's see bellow solution:

Example 1 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

var strJson = {
  "name": "MyWebtuts"
};

console.log(JSON.stringify(strJson)); 

now you can simply run by following command:

node server.js
Output :
{"name":"MyWebtuts"}

Example 2 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

var strJson = JSON.parse('{"MyWebtuts" : 5}');

console.log(strJson);

now you can simply run by following command:

node server.js
Output :
{ MyWebtuts: 5 }

It will help you...

#Node JS