The Wayback Machine - https://web.archive.org/web/20220827023129/https://www.geeksforgeeks.org/how-to-change-json-string-into-an-object-in-javascript/
Skip to content
Related Articles

Related Articles

How to change JSON String into an Object in JavaScript ?

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Aug, 2022
View Discussion
Improve Article
Save Article

What is JSON?

JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet. It appears close to OOP language like JavaScript but cannot be accessed like JavaScript. It consists of properties enclosed within flower braces.

Syntax :

{
    "prop1" : "value1",
    "prop2" : "value2"
     ...
}

In JavaScript, we write the same JSON content in the form of a string, and hence we cannot access the properties described in the JSON string as we do in objects.

const jsonString = ‘{“bookname” : “geekforgeeks”, “author” : “bharat”, “releaseYear” : “2001”}’;

This is what the JSON string looks like, we cannot manipulate it as it is immutable and hence we need to convert it purely into a JavaScript object so that we can access each individual property with a JavaScript object access method.

To convert it into a JavaScript object, we make use of a predefined object JSON which has a parse() method.

Syntax :

JSON.parse( JSONString );
  • This method takes a JSON string as an argument and returns a JavaScript object.

Example: In this example, we will change the above-considered JSON string into a JavaScript object and assign it to a variable named Javascriptprotest

var Jparse = JSON.parse( jsonString );

The results of this example will look like the following.

Jparse = { 
     bookname : "geekforgeeks", 
     author : "bharat",
     releaseYear : "2001"
}

It is a pure JavaScript object with three properties and the properties can be accessed as below,

Jparse.bookname 
// or
Jparse["bookname"]

You can see this result by using “console.log(Jparse)” on the debug screen of dev tools.

Example 1: Execute this below code with node.js. It shows how to convert the JSON content to objects after which you can access individual properties of the JSON using object access methods.

Javascript




const jsonString =
    `{"bookname" : "geekforgeeks", 
    "author" : "bharat", 
    "releaseYear" : "2001"}`;
const parsedString = JSON.parse(jsonString);
console.log("Geeksforgeeks");
console.log("How to change JSON content "
    + "to a JavaScript protest?\n");
console.log(jsonString);
console.log(parsedString);
  
console.log("Accessing individual properties :\n");
console.log(parsedString.bookname);

Output :

 

Example 2: Execute this below code with node.js. This code explains what happens if we access individual properties before parsing a  JSON string. It returns “undefined” instead of property values.

Javascript




const jsonString =
    `{"bookname" : "geekforgeeks", 
    "author" : "bharat", 
    "releaseYear" : "2001"}`;
console.log("Geeksforgeeks");
console.log("How to change JSON content"
    + " to a JavaScript protest?\n");
console.log(jsonString);
  
console.log("Accessing individual "
    + "properties without parsing :\n");
console.log(jsonString.bookname);
console.log(jsonString.author);

Output:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!