Play & JSON: How to transform a sequence of (String, JsValue) into a JsObject
json, playframework, scala
Solution
You were close enough. Use this:
val js = Json.obj("numbers" -> JsObject(numbers))
Output:
js: play.api.libs.json.JsObject = {"numbers":{"one":1,"two":2,"three":3}}
`Json.obj` is a shortcut to construct a `JsObject` and it is just not that convenient in your case.
Problem
Given the following scala sequence... ``` val numbers = Seq[(String, JsValue)](("one", JsNumber(1)), ("two", JsNumber(2)), ("three", JsNumber(3))) ``` ... I need to transform it into the following JSON: ``` { "numbers": { "one": 1, "two": 2, "three": 3 } } ``` I've tried this... ``` val js = Json.obj("numbers" -> Json.obj(numbers)) ``` ... but it doesn't work and I get the following error: ``` found: Seq[(String, JsValue)] required: (String, JsValueWrapper) ``` What am I doing wrong?