Ahmad Ahmad - 6 months ago 9
Javascript Question

JSON get user id's in nested array

How can I get all the user ids in the nested array JSON, I'm using the following code to get the JSON response:

var userListObj = JSON.stringify(result.items);
console.log(userListObj);


any help would be appreciated thank you in advance.



{
"current_page":1,
"per_page":100,
"total_entries":3,
"items":[
{
"user":{
"id":12729498,
"owner_id":51603,
"full_name":"test1",
"email":"test1",
"login":"test1",
"phone":null,
"website":null,
"created_at":"2016-05-16T14:56:24Z",
"updated_at":"2016-05-17T18:23:35Z",
"last_request_at":"2016-05-17T19:23:37Z",
"external_user_id":null,
"facebook_id":null,
"twitter_id":null,
"blob_id":null,
"custom_data":null,
"twitter_digits_id":null,
"user_tags":null
}
},
{
"user":{
"id":12768158,
"owner_id":51603,
"full_name":"test2",
"email":"",
"login":"test2",
"phone":null,
"website":null,
"created_at":"2016-05-17T18:25:13Z",
"updated_at":"2016-05-17T18:25:13Z",
"last_request_at":null,
"external_user_id":null,
"facebook_id":null,
"twitter_id":null,
"blob_id":null,
"custom_data":null,
"twitter_digits_id":null,
"user_tags":null
}
},
{
"user":{
"id":12769692,
"owner_id":51603,
"full_name":"test3",
"email":null,
"login":"test3",
"phone":null,
"website":null,
"created_at":"2016-05-17T19:22:55Z",
"updated_at":"2016-05-17T19:22:55Z",
"last_request_at":null,
"external_user_id":null,
"facebook_id":null,
"twitter_id":null,
"blob_id":null,
"custom_data":null,
"twitter_digits_id":null,
"user_tags":null
}
}
]
}




Answer

Like this:

var json = {  
   "current_page":1,
   "per_page":100,
   "total_entries":3,
   "items":[  
      {  
         "user":{  
            "id":12729498,
            "owner_id":51603,
            "full_name":"test1",
            "email":"test1",
            "login":"test1",
            "phone":null,
            "website":null,
            "created_at":"2016-05-16T14:56:24Z",
            "updated_at":"2016-05-17T18:23:35Z",
            "last_request_at":"2016-05-17T19:23:37Z",
            "external_user_id":null,
            "facebook_id":null,
            "twitter_id":null,
            "blob_id":null,
            "custom_data":null,
            "twitter_digits_id":null,
            "user_tags":null
         }
      },
      {  
         "user":{  
            "id":12768158,
            "owner_id":51603,
            "full_name":"test2",
            "email":"",
            "login":"test2",
            "phone":null,
            "website":null,
            "created_at":"2016-05-17T18:25:13Z",
            "updated_at":"2016-05-17T18:25:13Z",
            "last_request_at":null,
            "external_user_id":null,
            "facebook_id":null,
            "twitter_id":null,
            "blob_id":null,
            "custom_data":null,
            "twitter_digits_id":null,
            "user_tags":null
         }
      },
      {  
         "user":{  
            "id":12769692,
            "owner_id":51603,
            "full_name":"test3",
            "email":null,
            "login":"test3",
            "phone":null,
            "website":null,
            "created_at":"2016-05-17T19:22:55Z",
            "updated_at":"2016-05-17T19:22:55Z",
            "last_request_at":null,
            "external_user_id":null,
            "facebook_id":null,
            "twitter_id":null,
            "blob_id":null,
            "custom_data":null,
            "twitter_digits_id":null,
            "user_tags":null
         }
      }
   ]
};

var arr = [];
json.items.forEach(function(item) {
    arr.push(item.user.id);
});

console.log(arr);

Demo: https://jsfiddle.net/59tahwc6/1/