I am looking for a solution on how to create and/or append multiple fields to a play.api.libs.json.JsObject
. I have a list of (string -> JsValue) in which I don't know how many there are, nor specifically which field names are available.
Json.obj
does accepts multiple fields, but looks like you cannot pass in a list. Instead you need to specifically pass all the fields in like this:
Json.obj((k1 -> v1), (k2 -> v2), ...)
Which won't work for my use case. I would want something like this instead:
Json.obj(listOfFields) // listOfFields: List[(String, JsValue)]
Thank you in advance!
By having a look at the documentation, it's visible that JsObject
companion extends (Seq[(String, JsValue)]) ⇒ JsObject
, so rather than calling Json.obj
(with extra conversion):
import play.api.libs.json._
def foo(fields: List[(String, JsValue)]): JsObject = JsObject(fields)