Forum Discussion

nkpalli's avatar
nkpalli
Contributor
7 years ago

How to delete a key value pair from JSON response, goovy script

I have a groovy which basically make a rest request  as below sample and returns JSON response. but in the response returned  i would like to remove a key value pair(IsEditable = true)  before i star...
  • Radford's avatar
    Radford
    7 years ago

    The JsonSlurper returns Maps and Lists, thus with your second example you need to loop through each List item, which in turn is a Map and remove the item from each, for example:

     

    import groovy.json.JsonSlurper
    
    def Response = '''\
    [{
     "PVOptions" : "Yes, No",
     "UCount" : 449,
     "PName" : "Affordable Price All",
     "IsEdit" : true,
     "PCode" : "1234",
     "PPId" : 984,
     "IPNullable" : false
    },
    {
     "PVOptions" : "Yes, No",
     "UCount" : 4495,
     "PName" : "Affordable Price All",
     "IsEdit" : true,
     "PCode" : "12345",
     "PPId" : 984,
     "IPNullable" : false
    }]
    '''
    
    def slurperResponse = new JsonSlurper().parseText(Response)
    log.info("API Response results:" +  slurperResponse.toString())
    
    slurperResponse.each(){ dataSet ->
    	dataSet.remove('IsEdit')
    }
    
    log.info("API Response results:" +  slurperResponse.toString())