0

I have a JSON file from an API that I need to parse, however, I need to get the values of the expHistory. I have tried using double parse but all that does is return an error. A sample of the JSON file is:

"members": [{
    "uuid": "7a6885b0594148a0be32f713aa1e34b5",
    "rank": "Guild Master",
    "joined": 1516748711264,
    "quest_participation": 1632,
    "exp_history": {
      "2020-10-14": 0,
      "2020-10-13": 35262,
      "2020-10-12": 21614,
      "2020-10-11": 32067,
      "2020-10-10": 0,
      "2020-10-09": 14544,
      "2020-10-08": 4095
    },
    "muted_till": null
  },
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Mythbusters123
  • 100
  • 1
  • 7

1 Answers1

1

If your data looks like this after you've parsed the original JSON:

let data = {
    "members": [{
        "uuid": "7a6885b0594148a0be32f713aa1e34b5",
        "rank": "Guild Master",
        "joined": 1516748711264,
        "quest_participation": 1632,
        "exp_history": {
            "2020-10-14": 0,
            "2020-10-13": 35262,
            "2020-10-12": 21614,
            "2020-10-11": 32067,
            "2020-10-10": 0,
            "2020-10-09": 14544,
            "2020-10-08": 4095
        },
        "muted_till": null
    }]
};

Then, you can access the exp_history data like this:

let history = data.members[0].exp_history;
for (const [date, value] of Object.entries(history)) {
   console.log(`${date}: ${value}`);
}

And here's a working snippet:

    let data = {
        "members": [{
            "uuid": "7a6885b0594148a0be32f713aa1e34b5",
            "rank": "Guild Master",
            "joined": 1516748711264,
            "quest_participation": 1632,
            "exp_history": {
                "2020-10-14": 0,
                "2020-10-13": 35262,
                "2020-10-12": 21614,
                "2020-10-11": 32067,
                "2020-10-10": 0,
                "2020-10-09": 14544,
                "2020-10-08": 4095
            },
            "muted_till": null
        }]
    };

    let history = data.members[0].exp_history;
    for (const [date, value] of Object.entries(history)) {
       console.log(`${date}: ${value}`);
    }
jfriend00
  • 683,504
  • 96
  • 985
  • 979