[Vuejs]-How can I dynamically remove a parent in JSON with JS?

2👍

Assumed that you have the json saved in a variable ‘data’:

data = data.d
data.data = data.results
delete data.results

0👍

This function will do it.

function transform(json) {
  var obj = JSON.parse(json);
  obj.d.data = obj.d.result;
  delete obj.d.result;
  return JSON.stringify(obj.d);
}

0👍

One solution is to unserialize your JSON to have an object (JSON.parse()). Then to serialize only what you need (JSON.stringify()).

-1👍

You can use a loop.

var res = [];
for(var k in jsonData){
    res.push(jsonData[k]);
}
var jsonData = {
  "d": {
    "__count": "13",
    "results": [
      {
        "__metadata": {
          "id": "123"
        },
        "COAST": "East",
        "STATUS": "done",
        "COLOR": "blue",
      }
]
}
};

console.log(jsonData);

var res = [];
for(var k in jsonData){
    res.push(jsonData[k]);
}
console.log("result:");
console.log(res);

Leave a comment