Raj Nepali Raj Nepali - 5 months ago 14
JSON Question

how to remove only key from json object?

i have following json string.

{"Local People":{"label":"Local People","data":1},"Student":{"label":"Student","data":1}}

i want it to be formatted in following format

[{
"label" : "Student",
"data" : 1
},
{
"label" : "Student",
"data" : 1
}]

i have tried it many times but unable to change. please help.

Answer

After you have parsed the string with JSON.parse, you could take the keys of the object and iterate over the properties for a new array with the items.

var JSONstring='{ "Local People": { "label": "Local People", "data": 1 }, "Student": { "label": "Student", "data": 1 } }'
    object = JSON.parse(JSONstring),
    array = Object.keys(object).map(function(k) {
        return object[k];
    });

console.log(array);