How to get value from JSON object in JavaScript

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

Scroll to Top