1

I have a json file which contains json objects like following .

 {
  "RegionNames" : ["Region1", "Region2"],
  "StageNames" : ["Stage1", "Stage2"],
  "stages" : {
     "Stage1" : {
         "Region1" : {
            "var1": "value1",
            "var2": "value2"  
         },
         "Region2" : {
            "var1": "value1",
            "var2": "value2"
         }
      },
     "Stage2" : {
         "Region2" : {
            "var1": "value1";,
            "var2": "value2";
          }
      }
   }
}

I want to access this variable the var1 & var2 of both Region1 & Region2 in each stage .

//How i've tried accessing them in another typeScript file , the above content is in jsonContent.json : -

const stages =jsonContent.StageNames;
const regions = jsonContent.RegionNames;

for (let stageIndex in stages) {
    for (let regionIndex in regions) {
        console.log("Variable1 value :"+ jsonContent.stages[stageIndex].regions[regionIndex].var1)
        console.log("Variable2 value :"+
jsonContent.stages[stageIndex].regions[regionIndex].var2)
    }
}

Can someone please help me in this accessing in json using loop .

I need to use "RegionNames" & "StageNames" variables to run that loop , but not able to do so . Can someone please help .

3
  • let var = { means that it's not JSON. It also has syntax errors. Commented Jul 16, 2021 at 9:19
  • yeah , now removed it . Can you pin point other errors , if any . Commented Jul 16, 2021 at 9:21
  • May you also please update the question to include a minimal reproducible example of how you have tried accessing the properties? Commented Jul 16, 2021 at 9:22

1 Answer 1

1

You can use Object.entries() method on any object and convert it into array then use loops to access props and it's value ... Moreover I had to use forEach loop since your object isn't ideal ( meant to say that it contains something like item1 not item)..

let jsonContent = {
  "RegionNames": ["Region1",
    "Region2"],
  "StageNames": ["Stage1",
    "Stage2"],
  "stages": {
    "Stage1": {
      "Region1": {
        "var1": "value1",
        "var2": "value2"
      }
    },
    "Stage2": {
      "Region2": {
        "var1": "value1",
        "var2": "value2"
      }
    }
  }
};

const array = [];
Object.entries(jsonContent.stages).forEach(entry=> array.push(entry[1]));
array.forEach((item, index)=>{
    index++;
    const region = 'Region'+ index;
    const object = item[region];
    // console.log(object)
    console.log("Variable1 value :"+ object.var1);
    console.log("Variable2 value :"+ object.var2);
});

Note : you should never use indexes inside object like item1 as in objects of same data structure it is recommended to store data in easy way not using indexes with....

Sign up to request clarification or add additional context in comments.

4 Comments

@Shivanshu btw you should never write object with indexes ! Eg. Region1 could have been written as Region only ...
can you please elaborate this item & index thing that you used in loop .
@Shivanshu Okay I got it ! I used forEach loop a method in array by which we can loop through each item present in an array ..(see MDN for more info about it )...... The INDEX is the index of the item ( in array ) and the item is the thing present in the array
thanks , Got it . but this is throwing compile time error due to arrayType is any[] . As arrayType is nested , how can we provide it an appropriate type .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.