Home > Enterprise >  How can I get the multiple subchild objects from json using nodejs?
How can I get the multiple subchild objects from json using nodejs?

Time:10-05

I am trying to fetch the specifice object from my json files. For now I can fetch any tag instead of the "p" tag from it. you can please have look at the screenshot I have attached.

Click to open the json file

this is how I'm trying to fetch p tag:

'use strict';
const fs = require('fs');

var data = JSON.parse(fs.readFileSync('./me.json'));

data.Document.Decision.forEach(x => {
 console.log(x.Texte_Integral.p);
});

CodePudding user response:

This is a weird way of organizing your block, I recommend rewriting/reorganizing the JSON so it is more easily accessible.

There are a few things you have to know before this answer makes sense:

Array indexes

[] resembles an array, you can access each index by doing array[index], for example:

let arr = ['zero', 'one', 'two'];
console.log(arr[0]); //expected output: 'zero'

Bracket Notation

In JavaScript, there are two ways to access a variable's value, either by dot notation or bracket notation. As far as I know, the only differences these have are just the ability to use dynamic input and characters you can't usually use inside a variable's name, for example:

let obj = {
    var1: "this is variable1",
    var2: {
        var3: "this is variable3 inside variable2"
    }
}

console.log(obj.var1) //expected output: "this is variable1"
console.log(obj[`var1`]) // expected output: "this is variable1"

console.log(obj.var2.var3) //expected output: "this is variable3 inside variable2"
console.log(obj[`var2`].var3) // expected output: "this is variable3 inside variable2"
console.log(obj[`var2`]["var3"]) // expected output: "this is variable3 inside variable2"

Bracket notation also works inside objects, thus why the variable names inside as a string, like "Document", works.

let obj2 = {
    "var1": 1,
    ["var2"]: 2,
    var3: 3
};

console.log(obj2["var1"]) // expected output: 1
// console.log(obj2"var1") is INVALID and does not work
console.log(obj2["var2"]) // expected output: 2
console.log(obj2.var3) // expected output: 3

Coming to the solution

data.Document.Decision.forEach(x => {
    console.log(x.Texte_Integral[0].p)
});

This returns ["ASDFDSFDSFSD"], if we wanted to use it as a string and not an array (remember the brackets) then we would access the first index of the array. This would be done by adding [0] at the end.

❕ Solution

data.Document.Decision.forEach(x => {
    console.log(x.Texte_Integral[0].p[0])
});

Future Information

About stackoverflow, next time, please share code via code blocks and not screenshots. Thank you for coming to ask your question, we just want it to be easier for us to both understand and access the code! I had to type the whole thing out myself, which could've been easily avoided by just copy/paste-ing the code. If you don't know how to do certain things in the textbox, see the formatting help page.

  • Related