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
  },
5
  • 1
    Double parse? Does JSON.parse(myData) not work? Commented Oct 14, 2020 at 22:22
  • yourVarName.members[x].exp_history will give you your value, where x is some array index like 2,5,6, etc Commented Oct 14, 2020 at 22:29
  • I have tried to double parse, as i need the information from expHistory, and no, it returns an error Commented Oct 14, 2020 at 22:29
  • 2
    exp_history is just a property on an array item of your members array, so it would be accessible through members[0].exp_history Commented Oct 14, 2020 at 22:30
  • Related: How can I access and process nested objects, arrays or JSON? Commented Oct 15, 2020 at 2:04

1 Answer 1

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}`);
    }

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

Comments

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.