Home > Mobile >  modifying Json with Json Slurper in groovy
modifying Json with Json Slurper in groovy

Time:09-07

I have a scenario where i need to remove one object inside my Json string in groovy. below is the structure of my json. I need to remove "eventLogs" part from my Json.

{
  "_id":"123456",
  "type": "prod",
  "metaData" :{...},
  "isUpdated": true,
  "crossId":[...],
  "eventLogs":{...}
}

Expected

{
  "_id":"123456",
  "type": "prod",
  "metaData" :{...},
  "isUpdated": true,
  "crossId":[...]
}

I don't see any remove() function in jsonSlurpur to do this task. please advice.

def json = jsonSlurper.parseText(db.get(id).content().toString())

CodePudding user response:

JsonSlurper is a parser of json string to object (map/array) - map in your case.

After parsing you have to modify object and format it back to json string using JsonBuilder (or JsonOutput)

def obj = jsonSlurper.parseText(...)
obj.remove('eventLogs')

println new JsonBuilder(obj).toPrettyString()
  • Related