Reputation: 59
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
}
}
]
}
Upvotes: 1
Views: 49
Reputation: 2094
Here's my solution, commented and with error checking:
var x = '{"current_page":1, "per_page":100, "total_entries":3, "items":[{"user":{"id":12729498}},{"user":{"id":12768158}},{"user":{"id":12769692}}]}';
// Parse the JSON
var userListObj = JSON.parse(x);
// Check if any entries exist
if (typeof userListObj.items != 'undefined' && userListObj.items.length > 0)
{
// Loop through each user
for (var i in userListObj.items)
{
// Check if a user ID exists
if (typeof userListObj.items[i].user.id != 'undefined')
{
// Output the user ID to the console
console.log (userListObj.items[i].user.id);
}
}
}
Upvotes: 0
Reputation: 6580
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/
Upvotes: 0
Reputation: 11661
If you apply what @Mike C has commented you get something like this:
var formattedJson = JSON.parse(json);
for (i=0;i<formattedJson.items.length;i++){
console.log(formattedJson.items[i].user.id);
}
The formattedJson depends whether you get a JSON as string or as valid JSON, if so you don't need to use JSON.parse
, you can just access it.
Upvotes: 1