How To Iterate Json With Multiple Levels?
Welcome, I got a problem with JSON file. I would like to iterate through it and get every status into array, but I got stuck. I load this file and parse it. Then I tried to use for
Solution 1:
I assume this will be in javascript. You can try the following:
for (x in json[0].offers) {
console.log(json[0].offers[x].status);
}
Solution 2:
You have got an array Of Objects inside Array.
Firstly, you need to parse the data from JSON to object using JSON.parse(data);
Then, access the object offers
. using the parsedData[0].offers
object and then iterate over the array to get the status.
var data = `[{
"offers": [{
"advertiser_api_id": 12,
"status": 1
}, {
"advertiser_api_id": 13,
"status": 0
}]
}]`;
var parsedData = JSON.parse(data);
var result = [];
parsedData[0].offers.forEach((currentValue) => result.push(currentValue["status"]));
console.log(result)
Solution 3:
You can use map
function:
var data = [{
"offers": [{
"advertiser_api_id": 12,
"status": 1
}, {
"advertiser_api_id": 13,
"status": 0
}]
}]
var stats = data[0].offers.map(function(item) {
return item.status;
})
console.log(stats);
Solution 4:
This loops through the object and prints the data to console - you likely did not reach the correct array to loop through.
var data = [{
"offers": [{
"advertiser_api_id": 12,
"status": 1
}, {
"advertiser_api_id": 13,
"status": 0
}]
}];
data[0].offers.forEach(function(element) {
console.log(element.status);
});
Solution 5:
This will work
statuses = []
JSON.parse(data)[0].offers.forEach(x => statuses.push(x.status))
Post a Comment for "How To Iterate Json With Multiple Levels?"