I have a JSON like this:
{
"array1": [
{
"data": {
"id": "1",
"name": "foo",
},
"classes": "class1"
},
{
"data": {
"id": "2",
"name": "bar",
},
"classes": "class2"
...
}
}
I want to move all the classes into data object. Using https://jsoneditoronline.org I can only do that for each object individually. I guess I can write a script to do this, but is there a built tool for this?
CodePudding user response:
I don't think there will be any tool for such custom requirement. May be you can try the following script.
json.array1.forEach(arr => {
arr.data['classes'] = arr.classes;
delete arr.classes;
})
CodePudding user response:
It seems that regex is the quickest way to do this. Here is how I do:
- Search:
("data".*?)(}.*?)(,"classes":.*?)} - Replace:
$1$3$2}
