0

Following is a JSON output from an API, I've had trouble trying to compare the "last_save" variables, and if a profile has a larger "last_save" value, set the "profile_id" as a variable. (Im fairly new to java script, sorry if I use incorrect terminology)

Here is the code:

function profileID(){
    console.log("Running profileID.")
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
       rawJsonText = this.responseText;
       var parsedText = JSON.parse(rawJsonText);
       console.log(parsedText)
       console.log(playerUUID)
       lastPlayedIslandID = parsedText[0].last_save
       lastPlayedProfileName = parsedText.profile_id
       console.log(nameMC+"'s island ID : "+lastPlayedIslandID);
       console.log(nameMC+"'s island name : "+lastPlayedProfileName);
       slayerZombie();
      }
    };
    xhttp.open("GET", slothpixelAPI+"skyblock/profiles/"+nameMC, true);
    xhttp.send();

}

This is the JSON output from the API:

{
  "5ab88f71d10747aabf643e666c9933b1": {
    "profile_id": "5ab88f71d10747aabf643e666c9933b1",
    "cute_name": "Grapes",
    "first_join": 1578113432038,
    "last_save": 1582337480211,
    "collections_unlocked": 0,
    "members": [
      "4878f8a455e84956b19d4873d837ab93"
    ]
  },
  "4878f8a455e84956b19d4873d837ab93": {
    "profile_id": "4878f8a455e84956b19d4873d837ab93",
    "cute_name": "Coconut",
    "first_join": 1560932868602,
    "last_save": 1583315330184,
    "collections_unlocked": 59,
    "members": [
      "8d32864b3a364035922dd84d5247f483",
      "4878f8a455e84956b19d4873d837ab93"
    ]
  }
}

Thanks

Edwin
  • 35
  • 1
  • 4

2 Answers2

0

I i have understood,i think you have only one object with 2 fields. I think your parsedText returns something like:


[{
"5ab88f71d10747aabf643e666c9933b1": {"last_save": 1582337480211},
"4878f8a455e84956b19d4873d837ab93": {"last_save": 1583315330184}
}]

And parsedText[0] is like:

{
"5ab88f71d10747aabf643e666c9933b1": {"last_save": 1582337480211},
"4878f8a455e84956b19d4873d837ab93": {"last_save": 1583315330184}
}

You could access to the childrens like:

var text = '{"5ab88f71d10747aabf643e666c9933b1": {"last_save": 1582337480211},"4878f8a455e84956b19d4873d837ab93": {"last_save": 1583315330184}}'
obj = JSON.parse(text);
console.log(obj)

console.log(obj["4878f8a455e84956b19d4873d837ab93"]["last_save"])
console.log(obj["5ab88f71d10747aabf643e666c9933b1"]["last_save"])
Ángel Igualada
  • 819
  • 4
  • 11
0

parsedText is an object, not array, so number indexing isn't going to work. In fact objects don't order their values as you'd expect. Use Object.keys(parsedText) to get the names of the each value in the object, and iterate through them to find out what you need. There are other ways you can iterate through an object as well.

var parsedText = JSON.parse(rawJsonText);
var parsedKeys = Object.keys(parsedText);
var mostRecentProfile = "";

for(var i = 0; i < parsedKeys.length; i++) {
    if(mostRecentProfile) {
        if(parsedText[mostRecentProfile].last_save < parsedText[parsedKeys[i]].last_save) {
            // Current profile has more recent save
            mostRecentProfile = parsedKeys[i];
        }
    } else { // Set first profile as default
        mostRecentProfile = parsedKeys[i];
    }
}
console.log(parsedText[mostRecentProfile].last_save); // 1583315330184

A shorter way to do it if you're comfortable with one-liners is by getting only the values with Object.values(parsedText), sorting the objects by a property last_save, and selecting the first one.

var parsedText = JSON.parse(rawJsonText);
var mostRecentProfile = Object.values(parsedText).sort(function(a, b) {
    // Sort in descending order
    return b.last_save - a.last_save;
})[0].profile_id;
console.log(parsedText[mostRecentProfile].last_save); // 1583315330184
Pluto
  • 2,525
  • 22
  • 31