I have a JS object:
var source = {};
source.quantity = 1;
source.text = 'test';
Now I JSON it:
var json = JSON.stringify(source);
json looks like this:
{"quantity":"1","text":"test"}
I would like it to be like this:
[{"quantity":"1"},{"text":"test"}]
Ho can I do this?
Get all the keys as an Array them map them to Objects as key-value pairs from source
JSON.stringify(
Object.keys(source)
.map(
function (e) {
var o = {};
o[e] = source[e];
return o;
}
)
); // "[{"quantity":1},{"text":"test"}]"