JavaScript parsing JSON


In this short tutorial we will learn how to parse a JSON string to a JavaScript object.

The code

We will start by defining a string literal containing a valid JSON object. This JSON object will be very simple, containing two properties:

  • name: a string;
  • age: a number.
const jsonString = `{
  "name": "Todd",
  "age": 20    

Then, to parse this string to a JavaScript object, we simply need to call the parse method on the JSON built-in object. As input, this method receives the JSON string and, as output, it returns the corresponding JavaScript object

const obj = JSON.parse(jsonString);

To confirm the object was correctly parsed, we will print its properties to the console.


Upon running the previous code, we should get an output similar to figure 1. As can be seen, the values of both properties of the object were correctly printed.

Properties of the parsed object printed to the console.
Figure 1 – Properties of the parsed object printed to the console. Tested with Visual Studio Code and the Code Runner extension.

Related Posts

Leave a Reply