Prune multiple JSON branches with play-json
Imagine you want to prune/drop/remove the id and owner branches from this json payload
{
"attr1": "value1",
"attr2": "value2",
"attr3": "value3",
"owner": "foo@bar.com",
"id" :{ "$oid":"aaaaaaaa"}
}
When transforming json with play-json transformers if you want to prune branches you must use the following composition :
val jsonDropKey= (__ \ 'id ).json.prune andThen
(__ \ 'owner ).json.prune
The following will NOT work:
val jsonDropKey= ((__ \ 'id ).json.prune and
(__ \ 'owner ).json.prune).reduce
The reason is that the and ... reduce
syntax will apply each transformers to the whole input and merge (reduce) the result. On the contrary andThen
will apply the transformers sequentially passing the result of one transformer on to the next.
Written by Jean Helou
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#