How to get value from JSON object in JavaScript

featured Image

In this article, we will learn how to get a value from a JSON object in JavaScript.

Suppose we have to fetch some data from a server, and we get the data in JSON format. The JSON data we received from the server is usually in String format.

So to get a particular value of the property from the JSON string we have to first convert it to a JavaScript object.

And then using the dot (.) operator or square bracket accessor we can get the value of the property.

Let’s see an example

Get a value of a property/key from JSON object

We can get the value of the JSON object in two steps:

  1. First, we have to convert the JSON string to a JavaScript object using JSON.parse() method.
  2. And then we can use dot operator (.) or square bracket notation([ ]) to get the value of the property.

Example:

var jsonObj = `{  
    "employee": {  
        "name":       "jack",   
        "salary":      56000,   
        "married":    true  
    }  
}`

var parseJson = JSON.parse(jsonObj)

console.log(parseJson.employee.name) // using dot operator
console.log(parseJson['employee']['salary']) // using square bracket

Output:

jack
56000

Related Topics:

How To Covert JavaScript Object To JSON String

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more