Problem
I have the following example json data which is not formatted how I need it:
"stations": {
"st1": "station 1",
"st2": "station 2",
"st3": "Station 3",
}
Question
How can I reformat the data to be:
"stations": [
{
"id": "st1",
"name": "station 1",
},
{
"id": "st2",
"name": "station 2",
},
{
"id": "st3",
"name": "station 3",
}
]
Tried
I tried to simply log the data to test first but am struggling how to actually even iterate between the key/value pairs as they are essentially strings
This is what i tried:
$.get( '/js/ajax/tube-data.json', function( data ) {
$.each(data.stations, function () {
// I was expecting st1, st2, st3 to show in the console
// but got first letter of each station
console.log(this[0])
});
}).error(function() {console.log(arguments) });
Is there a better way to do this?