0

I'm trying to pull out some json info into a table but the first object doesn't have a label like "name" it has the actual value which could be anything

{
   "person": {
           "Clark": [{
           "lastName": "Kent",
           "job": "Reporter",
           "roll": 20
           },{
              "alterEgo": "Superman",
              "powers":["strength", "lasereyes", "hair"]
           }
   ],
           "Bruce": [{
           "lastName": "Wayne",
           "job": "Playboy",
           "roll": 30
           },{
            "alterEgo": "Batman",
            "powers":["fighting", "gadgets", "money"]
         }
 ],
           "Peter":  [{
           "lastName": "Parker",
           "job": "Photographer",
           "roll": 40
           },{
            "alterEgo": "Spider-man",
            "powers":["strength", "arm-goo", "wit"]
         }
 ]
}
}

Example table

Name alterego Job
clark Superman Reporter
Bruce Batman Playboy
Peter Photographer Spider-man

So I don't know how to get Clark, Bruce, and Peter into the first column of my table since I can't reference them by "name" or "firstname" or anything? And the names could be anything. Also this is just the part of the json I care about there is a lot before and after that I don't care about. Since its just a chunk of a larger json I don't think its seen as an array. Any help would be appreciated.

thinguy
  • 53
  • 7
  • First step would be to convert the JSON into a JS object. `const data = JSON.parse(jsonString)`. `Object.keys(data.person)` may give you the names of the people? – evolutionxbox Jun 22 '21 at 16:25
  • [Object.entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) – Charlie Bamford Jun 22 '21 at 16:27
  • 1
    wow, that is a _horrible_ format of data. Items in an array should be homogeneous which these arnt, so you're stuck with expecting the arrays to all follow a similar pattern. – Jamiec Jun 22 '21 at 16:33

2 Answers2

2

You can use Object.entries to get the keys/values from your data.person object, but then due to the nature of the array underneath that you're stuck with expecting job to be in the first element and alterEgo being in the second - as soon as that changes/goes out of order you're stuck with having to look for them individually.

Assuming everything remains uniform this is fairly easy:

const data = {
  "person": {
    "Clark": [{
      "lastName": "Kent",
      "job": "Reporter",
      "roll": 20
    }, {
      "alterEgo": "Superman",
      "powers": ["strength", "lasereyes", "hair"]
    }],
    "Bruce": [{
      "lastName": "Wayne",
      "job": "Playboy",
      "roll": 30
    }, {
      "alterEgo": "Batman",
      "powers": ["fighting", "gadgets", "money"]
    }],
    "Peter": [{
      "lastName": "Parker",
      "job": "Photographer",
      "roll": 40
    }, {
      "alterEgo": "Spider-man",
      "powers": ["strength", "arm-goo", "wit"]
    }]
  }
}

Object.entries(data.person).forEach( ([name,details]) => {
   console.log(name, details[0].job, details[1].alterEgo)
});
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

Here is another way you can do this :

const jsonData = `{
  "person": {
     "Clark": [
       {
         "lastName": "Kent",
         "job": "Reporter",
         "roll": "20"
       },
       {
         "alterEgo": "Superman",
         "powers":["strength", "lasereyes", "hair"]
       }
     ],
     "Bruce": [
       {
         "lastName": "Wayne",
         "job": "Playboy",
         "roll": "30"
       },
       {
         "alterEgo": "Batman",
         "powers":["fighting", "gadgets", "money"]
       }
     ],
     "Peter": [
       {
         "lastName": "Parker",
         "job": "Photographer",
         "roll": "40"
       },
       {
         "alterEgo": "Spider-man",
         "powers":["strength", "arm-goo", "wit"]
       }
     ]
  }
}`

const data = JSON.parse(jsonData)

for (const person in data.person) {
  console.log(person, data.person[person])
}
pierre-lgb
  • 842
  • 1
  • 6
  • 15
  • Thanks guys. I've run into another issue. The json I'm dealing with sometimes changes the order results for person. For example lets say the lastName, job, and roll are in one array, and the alterEgo and powers are in another array But the order of those arrays can change under the person so sometimes person[0].lastName will work but other times its person[1].lastName. So if I search the results will be fine until it hits one thats flipped and throw an undefined. Hopefully that makes sense. I've googled, and tried various things but can't find a way for both options to work. – thinguy Jun 25 '21 at 16:56