Jump to content

Javascript loop through staggered JSON object

venomtail

I take in json data that I want to go through and use the fields that I need.

Data looks like this

{
  "Part1": {
    "Field_1": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_2": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_3": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_4": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_5": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_6": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_7": {
      "Type": "radio",
      "Name": "RadioDefault",
      "special": "for"
    },
    "Field_8": {
      "Type": "radio",
      "Name": "RadioDefault",
      "special": "for"
    },
    "Field_9": {
      "Type": "radio",
      "Name": "RadioDefault",
      "special": "for"
    }
  },
  "Part2": {
    "Field_1": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_2": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_3": {
      "Type": "",
      "Name": "TargetID38",
      "special": "rows=\"2\""
    },
    "Field_4": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_5": {
      "Type": "",
      "Name": "",
      "special": ""
    },
    "Field_6": {
      "Type": "",
      "Name": "",
      "special": ""
    }
  }
}

I've parsed the whole json into an object

certElementData = Object.entries(JSON.parse(result));

And tried to loop through the data.

 

Getting unlucky here. Maybe I should go back to simple for loops but I want to try out the for in and for of loops.

 

For of loops do well until I get to the Field_'s part as that refuses to go into a array and stays being an object. If I try to use a for in loop, that loop will wipe any following data, saying that sure it'll now loop through to Field_9 but that's it, there's no data after it.

 

How can I retain all the data yet get to the bottom info such as "Name":"" and "Type":"" that I need.

 

Have a look at the initial start parse as well. What if I'm needlessly parsing them and made a mistake at the start?

 

Thanks.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm starting to think the json itself is set up wrong.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, venomtail said:

Data looks like this

Your data is an object. Not an array. JSON encoded arrays will have square brackes. Objects have curly braces

 

1 hour ago, venomtail said:

For of loops do well until I get to the Field_'s part as that refuses to go into a array and stays being an object. If I try to use a for in loop, that loop will wipe any following data, saying that sure it'll now loop through to Field_9 but that's it, there's no data after it.

For of is for iterating over array's.

For in for loop though an objects properties.

 

You can convert your data object into an array using Object.entries(data)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

Link to comment
Share on other sites

Link to post
Share on other sites

Use Object.Keys() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

 

You can do

const object1 = {
  a: 'somestring',
  b: 42,
  c: false,
};

for (let x of Object.keys(object1)) {
  console.log(x + " -> " + object1[x]);
}

Edit:

Yeah, entries() should work as well. Try this:

const object1 = {
  a: 'somestring',
  b: 42,
  c: {
    d: "d",
    e: "e"
  }
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×