0

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())

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

thanks daggett... did similar thing but forgot to convert it to jsonString. will do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.