N3da N3da - 2 months ago 5
Scala Question

Remove a key from a JsValue in Scala

It's probably a very easy question but I'm having trouble finding a clean/working solution.
I just want to remove a field from a json object I have.
Let's say I have :

val body:Option[JsValue] = request.body.asJson


where body looks like:

{
"url": "www.google.com",
"id": "123",
"count" : 1,
"label" : "test"
}


and I want to remove the field "id" from it.

I have read http://www.playframework.com/documentation/2.1.1/ScalaJsonTransformers case#6 but unfortunately couldn't fully understand it. (I'm pretty new to Scala and functional programming)

Thanks!

m-z m-z
Answer

This can be done as a JsObject, which extends JsValue:

body.as[JsObject] - "id"