mo0206
mo0206

Reputation: 801

remove a key from map

The below json is of type class java.util.HashMap

jsonRequest=[noOfMembers:2, coverageYear:2017, zipCode:99123, premiumList:[[Premium:203.05, Id:1000101], [Premium:205.36, Id:1000102], [Premium:207.67, Id:1000103], [Premium:209.98, Id:1000104], [Premium:212.29, Id:1000105]], members:[[id:5487, age:34, gender:M], [id:5488, age:32, gender:F]]]

I am trying to remove premiumList from the below map by using jsonRequest= jsonRequest.remove("premiumList") in groovy.

and when I print the jsonRequest it shows the output as

[{Premium=203.05, Id=1000101}, {Premium=205.36, Id=1000102}, {Premium=207.67, Id=1000103}, {Premium=209.98, Id=1000104}, {Premium=212.29, Id=1000105}]

how can i remove entire premiumList from my map in groovy?

Upvotes: 0

Views: 62

Answers (1)

fsi
fsi

Reputation: 1367

Even though your java.util.HashMap is hashmap and you tried to remove a key , you need to know where your jsonRequest comes from. AFIK, you tried to remove a key from request which it doesn't allow to remove?

def jsonRequest = [noOfMembers:2, coverageYear:2017, zipCode:99123, premiumList:[[Premium:203.05, Id:1000101], [Premium:205.36, Id:1000102], [Premium:207.67, Id:1000103], [Premium:209.98, Id:1000104], [Premium:212.29, Id:1000105]], members:[[id:5487, age:34, gender:M], [id:5488, age:32, gender:F]]]
​def output = [:] << jsonRequest

output.remove('premiumList')​

Upvotes: 2

Related Questions