Categories
JavaScript Answers

How to access nested JSON data with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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