Reputation: 9734
Given the following sequence of JsValue
instances:
[
{ "name":"j3d" },
{ "location":"Germany" }
]
How do I merge them to a single JSON document like this?
{
"name":"j3d",
"location":"Germany"
}
Here below is my Scala code:
import play.api.libs.json._
val values = Seq(Json.obj("name":"j3d"), Json.obj("location":"Germany")
how do I merge all the JSON objects in values
?
Upvotes: 2
Views: 1850
Reputation: 3122
You can use a fold and deepMerge
:
values.foldLeft(Json.obj())((obj, a) => obj.deepMerge(a.as[JsObject]))
Upvotes: 9