Two assumptions are made here:
- That the first result of your JSON is the specification of the keys
- That the results always follow the same order (e.g. index 0 is always the
Type
field).
With those two assumptions, it’s easy to do this with a pair of nested for-loops. Could get fancier with ES5 methods, but the index orders are important here (since your JSON depends on order of values) so I stuck with regular for
loops. Personally, I would reformat your JSON if possible to have it return in an easier format to deal with.
var json = '{"DataStats":[["Type","Name","valueDateTime","value","attr0","attr1","attr2"],["AA","End Time","10/4/2014 12:00:00 AM","01:10:23.37","USA_325","expected","AA_overall"],["AA","End Time","10/4/2014 12:00:00 AM","01:10:23.37","USA_325","expected","AA_overall"]]}';
var parsed = JSON.parse(json).DataStats;
var keys = parsed[0];
var result = [];
for (var i = 1; i < parsed.length; i++) {
var aux = {};
for (var j = 0; j < keys.length; j++) {
aux[keys[j]] = parsed[i][j];
}
result.push(aux);
}
console.log(result);
0
solved JSON deserialization in javascript