1

I've been trying to wrap my head around my first proper site using JSON data and an API; I'm using Destiny 2's API - but I've now hit a bit of snag.

I'm not sure how to loop through, for example, the membershipId of each Response.characters.data.(?).membershipId. The (?) is dynamic, depending on the original API request, but I'm not sure how to loop through this since it's not in an array.

 {
    "Response": {
        "characters": {
            "data": {
                "2305843009301086354": {
                    "membershipId": "4611686018467309748",
                    "membershipType": 4,
                    "characterId": "2305843009301086354",
                    "dateLastPlayed": "2018-08-27T19:22:17Z",
                    "minutesPlayedThisSession": "166",
                    "minutesPlayedTotal": "1251",
                    "light": 300
                },
                "2305843009299512096": {
                    "membershipId": "4611686018467309748",
                    "membershipType": 4,
                    "characterId": "2305843009299512096",
                    "dateLastPlayed": "2018-08-28T22:27:07Z",
                    "minutesPlayedThisSession": "58",
                    "minutesPlayedTotal": "4115",
                    "light": 359
                },
                "2305843009317224837": {
                    "membershipId": "4611686018467309748",
                    "membershipType": 4,
                    "characterId": "2305843009317224837",
                    "dateLastPlayed": "2017-12-08T19:34:05Z",
                    "minutesPlayedThisSession": "12",
                    "minutesPlayedTotal": "12",
                    "light": 10
                }
            },
            "privacy": 1
        },
        "characterUninstancedItemComponents": {},
        "itemComponents": {}
    },
    "ErrorCode": 1,
    "ThrottleSeconds": 0,
    "ErrorStatus": "Success",
    "Message": "Ok",
    "MessageData": {}
}

How do I target this? I was expecting something similar to how I'd do an array, something along the lines of:

var characterIds = characterData.Response.characters.data;

   for(var i = 0; i < characterIds.length; i++){
     var characterId = characterIds[i];
     var characterMembershipId = characterId.membershipId; 
     console.log(characterMembershipId);
   }

Any help would be appreciated here - sorry if my explanations are a bit naff, my terminology isn't great: trying to get to grips and learning things by getting stuck in!

EDIT: Thanks for the help so far guys, I think I might've done a bad job of explaining what I'm after.

The part I'm trying to get from a For loop is the results of Response.characters.data, the random numbers (currently "2305843009301086354", "2305843009299512096" and "2305843009317224837") - which changes depending on the API request so:

{
    "Response": {
        "characters": {
            "data": {
                "2305843009301086354": {
                   "characterId": "2305843009301086354",
                },
                "2305843009299512096": {
                    "characterId": "2305843009299512096",
                },
                "2305843009317224837": {
                    "characterId": "2305843009317224837",
                }

and then from there, I want to be able to console.log the characterId for each loop of these .data results.

Sorry if that makes even less sense, but I really appreciate the help so far!

SECOND EDIT: Apologies guys, I was being thick - got it working, really appreciate the help!

CKE
  • 1,533
  • 19
  • 18
  • 29
Cpotey
  • 23
  • 3

2 Answers2

0

Iterate over the Object.values of the data property to get to each member object, then extract the membershipId of each:

const input={"Response":{"characters":{"data":{"2305843009301086354":{"membershipId":"4611686018467309748","membershipType":4,"characterId":"2305843009301086354","dateLastPlayed":"2018-08-27T19:22:17Z","minutesPlayedThisSession":"166","minutesPlayedTotal":"1251","light":300},"2305843009299512096":{"membershipId":"4611686018467309748","membershipType":4,"characterId":"2305843009299512096","dateLastPlayed":"2018-08-28T22:27:07Z","minutesPlayedThisSession":"58","minutesPlayedTotal":"4115","light":359},"2305843009317224837":{"membershipId":"4611686018467309748","membershipType":4,"characterId":"2305843009317224837","dateLastPlayed":"2017-12-08T19:34:05Z","minutesPlayedThisSession":"12","minutesPlayedTotal":"12","light":10}},"privacy":1},"characterUninstancedItemComponents":{},"itemComponents":{}},"ErrorCode":1,"ThrottleSeconds":0,"ErrorStatus":"Success","Message":"Ok","MessageData":{}}

Object.values(input.Response.characters.data)
  .forEach(({ membershipId }) => {
    console.log(membershipId);
  });
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Here try this, use jquery "for" to iterate over characters and finally print their id

var response = {
"Response": {
    "characters": {
        "data": {
            "2305843009301086354": {
                "membershipId": "4611686018467309748",
                "membershipType": 4,
                "characterId": "2305843009301086354",
                "dateLastPlayed": "2018-08-27T19:22:17Z",
                "minutesPlayedThisSession": "166",
                "minutesPlayedTotal": "1251",
                "light": 300
            },
            "2305843009299512096": {
                "membershipId": "4611686018467309748",
                "membershipType": 4,
                "characterId": "2305843009299512096",
                "dateLastPlayed": "2018-08-28T22:27:07Z",
                "minutesPlayedThisSession": "58",
                "minutesPlayedTotal": "4115",
                "light": 359
            },
            "2305843009317224837": {
                "membershipId": "4611686018467309748",
                "membershipType": 4,
                "characterId": "2305843009317224837",
                "dateLastPlayed": "2017-12-08T19:34:05Z",
                "minutesPlayedThisSession": "12",
                "minutesPlayedTotal": "12",
                "light": 10
            }
        },
        "privacy": 1
    },
    "characterUninstancedItemComponents": {},
    "itemComponents": {}
},
"ErrorCode": 1,
"ThrottleSeconds": 0,
"ErrorStatus": "Success",
"Message": "Ok",
"MessageData": {}
};

var data = response;

$characters = data.Response.characters.data;
for($character in $characters)
{
  console.log($characters[$character].membershipId);
}
Sandesh Satyal
  • 196
  • 1
  • 8