How to access nested JSON data with JavaScript?

Sometimes, we want to access nested JSON data with JavaScript.

In this article, we’ll look at how to access nested JSON data with JavaScript.

How to access nested JSON data with JavaScript?

To access nested JSON data with JavaScript, we can use the Lodash get method.

For instance, we write:

const data = {
  "id": 1,
  "name": "abc",
  "address": {
    "streetName": "cde",
    "streetId": 2
  }
}
const streetName = _.get(data, 'address.streetName');
console.log(streetName)

to define the data object.

Then we get the value of the data.address.streetName property with _.get(data, 'address.streetName');.

As a result, streetName is 'cde'.

Conclusion

To access nested JSON data with JavaScript, we can use the Lodash get method.