How to extract values from nested JSON

In this tutorial, you will learn how to extract values from nested JSON using various methods in Node.js. These methods include dot notation, square bracket notation, and object destructuring. The concepts discussed here can be applied in other programming languages with slight syntax variations.

Before we dive in, it’s important to note that in Node.js, JSON data can be directly written as a JavaScript object with or without the need for backticks (`) and JSON.parse().

Example:

let data = `{
  "isbn": "123-456-222",
  "author": {
    "lastname": "Doe",
    "firstname": "Jane"
  },
  "editor": {
    "lastname": "Smith",
    "firstname": "Jane"
  },
  "title": "The Ultimate Database Study Guide",
  "category": ["Non-Fiction", "Technology"]
}`;

let d = JSON.parse(data); // `d` now holds the JSON data as an object.

Dot notation:

It is the most basic method for extracting values from JSON. In this method, we access values with the dot operator. Suppose we want to extract the author’s first name then we need to first access the author part with the dot operator, it seems like this- d.author. And now we have to access the first name of the author,  we just have access to the author d.author and now with the help of it, access to the first name looks like this- d.author.firstname.

let AuthorFirstName = d.author.firstname;
console.log(AuthorFirstName); // Output: Jane

Another example of accessing the author’s last name looks like this- `d.author.lastname`

let AuthorLastName=d.author.lastname;
console.log(AuthorLastName);  //Smith

Square bracket notation:

This method is almost the same as dot notation, here the only difference is we use square brackets instead of the dot operator. Consider the previous example if we need to access the author’s first name, we need to access the author first and it looks like this- [d][author]. and then we will access the first name and overall after accessing it looks like-[d][author][firstname].

let AuthorFirstName=[d][author][firstname];
console.log(AuthorFirstName); //Jane

For another example, if we want to access the author’s last name it looks like this- [d][author][lastname].

let AuthorLastName=[d][author][lastname];
console.log(AuthorLastName); //Doe

Object Destructuring:

This method is a little bit different from them. In this method, we directly use the destructuring method of objects. If we want the value of the last name of the editor then we directly write, here we are accessing the values of the editor now we need to access the last name like this – const{editor:{lastname: value}}after this, we equalize it dto get the value. Here the ‘value’ is the required value for the editor’s last name.

const {editor: { lastname: value },} = d;
console.log(value); //Smith const

Similarly to access the category we write like this-const { category: value} , here we will get an Array.

{ category: v } = d; 
console.log(v); //["Non-Fiction", "Technology"]

Conclusion:

These are the basic methods to extract values from nested JSON objects. Each method has its use cases, so choose the one that fits your specific scenario. With these techniques, you can efficiently work with complex JSON data in your applications.

Remember to pay attention to syntax and ensure you’re using the correct key names when accessing nested values. Happy coding!

Leave a Reply

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