JavaScript

How to parse JSON into JavaScript

In this example, we will see how to parse a JSON string in JavaScript. The main use of JSON is to exchange data between a client and a server. When we receive data from a web server, the data is always a string. So we need to parse the data with JSON.parse() and the data becomes a JavaScript object.

If you have JSON data in a string, the best way to parse it is to use the JSON.parse() method. JSON.parse() is synchronous. So the larger the JSON data, the more your program execution will be blocked until the JSON parsing is complete.
 

 

How to parse JSON into JavaScript

Suppose we received the following text from a web server:

'{"name": "Yohan", "age": 25, "country": "USA"}'

Now use the JSON.parse() function to convert this string to a JavaScript object.

let obj = JSON.parse('{"name": "Yohan", "age": 25, "country": "USA"}');

Now the whole string becomes a JS object and we can access its values through its properties.

console.log(obj.country); // USA
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *