javascriptvue.jsvue-tables-2

How can I dynamically remove a parent in JSON with JS?


With some JavaScript, how can I transform a JSON from:

{
  "d": {
    "__count": "13",
    "results": [
      {
        "__metadata": {
          "id": "123"
        },
        "COAST": "East",
        "STATUS": "done",
        "COLOR": "blue",
      }
]
}
}

TO

{
    "__count": "13",
    "data": [
      {
        "__metadata": {
          "id": "123"
        },
        "COAST": "East",
        "STATUS": "done",
        "COLOR": "blue",
      }
]
}

Basically removing the extra "d" parent and renaming results to data? I am using this in the context of vue-table in VueJS.


Solution

  • Assumed that you have the json saved in a variable 'data':

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