jsongroovyjsonslurper

groovy to remove json unwated array label


I have this json and I want to remove the field item.

{ "field": "AAA", "list": { "item": [ { "field01": "111", "field02": "222" }, { "field01": "333", "field02": "444" } ] }}

I'm using this json slurper groovy but it's returnung null.

def myJson = '..' //above json; def jsonParser = new JsonSlurper(); def jsonObject=jsonParser.parseText(myJson); return JsonOutput.toJson(jsonObject["item"])

The expected output is:

{ "field": "AAA", "list": [ { "field01": "111", "field02": "222" }, { "field01": "333", "field02": "444" } ]}

How can I do to remove the field "item"?


Solution

  • def myJson = '..' //above json; 
    def jsonParser = new JsonSlurper(); 
    def jsonObject=jsonParser.parseText(myJson); 
    jsonObject.list=jsonObject.list.item
    return JsonOutput.toJson(jsonObject)