0

I have to iterate through this JSON:

{
  "data": 321563,
  "group": [
    {
      "added": 42421,
      "normal": {
        "x": 39,
        "y": "0.1300",
        "b": "0.4326",
        "c": "0.0552",
        "f": 166833
      },
      "j": "240313",
      "b": "0.2251",
      "a": "dda",
      "b": "0.101",
      "a": 922,
      "f": {
        "c": 39,
        "d": "0.263",
        "a": "2.8955",
        "h": "0.3211",
        "d": 274
      },
      "a": false,
      "k": 5,
      "w": "0.072",
      "d": "0.045",
      "e": 3
    },

I only want the j and k stored like a key value pair e.g. "j":k

I need to loop all of it, and store it to a file.

Spaceglider
  • 97
  • 11
  • 1
    What specifically are you having problems with? Do you know how to iterate over an array? – Felix Kling Mar 18 '19 at 20:19
  • 1
    That's not JSON, it's an object. JSON is a string format. – Scott Marcus Mar 18 '19 at 20:20
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/q/11922383/218196) – Felix Kling Mar 18 '19 at 20:51
  • Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC-BY-SA 3.0 license](//creativecommons.org/licenses/by-sa/3.0)). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request](//meta.stackoverflow.com/q/323395/584192)? – Samuel Liew May 06 '19 at 13:15

2 Answers2

0

You can use a map to get a new array of items, this will not affect the old array.

const data = {
  "game_count": 8750,
  "sets": [
    {
      "appid": "221540",
      "true_count": 9,
      "bgs_avg": "0.10",
      // Other data here
    },
    {
      "appid": "123456",
      "true_count": 9,
      "bgs_avg": "0.20",
      // Other data here
    }
  ]
}


// Use "data.sets = data.sets.map(...)" to replace the data
// The following will only assign to a new variable
const newArray = data.sets.map(itm => { return {appid: itm.appid, true_count: itm.true_count} })

console.log(newArray)

We can also take the data and assign it directly back to the original overwriting it just by using data.sets = data.sets.map(...) as seen here:

const data = {
  "game_count": 8750,
  "sets": [
    {
      "appid": "221540",
      "true_count": 9,
      "bgs_avg": "0.10",
      // Other data here
    },
    {
      "appid": "123456",
      "true_count": 9,
      "bgs_avg": "0.20",
      // Other data here
    }
  ]
}

data.sets = data.sets.map(itm => { return {appid: itm.appid, true_count: itm.true_count} })

console.log(data)
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

In simple javascript this should work -

let newObj = {}
for(let i=0; i<obj.group.length; i++){
  newObj[obj.group[i].j] = obj.group[i].k 
}

Where 'obj' is your object

newObj will be you new Object which will contain all the key value pair

Spaceglider
  • 97
  • 11
Shaurya Mittal
  • 764
  • 1
  • 8
  • 19