I’m dealing with a fairly complex JSON array, included below. I’m trying to create an array of the Country objects with their included visits. Heres a sample of the array:
{
"data":[
{
"period":"Month",
"start_date":"2012-06",
"end_date":"2012-07",
"attributes":{
},
"measures":{
"Visits":1000000
},
"SubRows":[
{
"Unknown":{
"measures":{
"Visits":1000
},
"SubRows":null
},
"**":{
"measures":{
"Visits":1000
},
"SubRows":null
},
"Afghanistan":{
"measures":{
"Visits":1000
},
"SubRows":null
},
"Aland Islands":{
"measures":{
"Visits":1000
},
"SubRows":null
},
"Albania":{
"measures":{
"Visits":100
},
"SubRows":null
},
}
]
}
]
}
I’m using data[0].SubRows[0] to get down to an array of the country objects, but now I’m stumped how to go one below as each sub object is named differently? My intended output is for the Google Visualisation API, as follows:
var data = google.visualization.arrayToDataTable([
['Country', '% Visits'],
['United Kingdom', 1000],
['United States', 1000],
['France', 1000],
['Australia', 1000],
]);
Glibnes is getting there with the
for insuggestion, sticking to your variable names (BTW: in JS, these are objects, not arrays… well, arrays are objects, but I’m not going to be that pedantic).measureswill now be a one level object, where the countries are the keys and the values are the Visits. Feel free to add aditional checks, or use the values directly to build content or… whateverWhat you really need, if I understand correctly though, is an array of arrays:
resultwill now be an array, consisting of arrays, each with 2 values.result[x][0]-> country andresult[x][1]-> value of measures.Visits: