2

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?

j3d
  • 9,492
  • 22
  • 88
  • 172

1 Answers1

9

You can use a fold and deepMerge:

values.foldLeft(Json.obj())((obj, a) => obj.deepMerge(a.as[JsObject]))
edi
  • 3,112
  • 21
  • 16