You can't have the output you want.
[{
name:xxxx
key:population
value:232
key:marginal
value:24
},
{
name:yyyyy
key:population
value:6372
key:marginal
value:566
}]
In javascript and usually all hashtables, you cannot have multiple keys with the same name.
What you want is something like this:
[{
"name":"xxxx",
"population": 232,
"marginal": 24
},
{
"name":"yyyyy",
"population": 6372,
"marginal": 566
}]
Or something like this:
[{
"name":"xxxx",
"keys": [
["population", 232],
["marginal", 24]
]
},
{
"name":"yyyyy",
"keys": [
["population", 6372],
["marginal", 566]
]
}]
To get the second output, iterate over all your results. You need some kind of collection in which you can save the objects you need for output. For each line, check if there is already an object created. If not create one, if there is one add to the "keys" array.
obj.keys.push([current.key, current.value]);
Then when you finished processing each objects you should be able to dump a json out of the js object.