j3d
j3d

Reputation: 9734

PlayFramework: How to merge a sequence of JsValue instances to a single JSON document

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

Answers (1)

edi
edi

Reputation: 3122

You can use a fold and deepMerge:

values.foldLeft(Json.obj())((obj, a) => obj.deepMerge(a.as[JsObject]))

Upvotes: 9

Related Questions