You can also use Json library macros to convert case classes to Json
import play.api.libs.json._ case class MyObject(id: String, name: String, data: JsObject = Json.obj(), children: Seq[MyObject]) implicit val myObjectFormat = Json.format[MyObject]
Then, when you need the Json version of the case MyObject class, you can simply run:
val obj = MyObject("node37", "3.7", Json.obj(), Seq()) val jsonObj = Json.toJson(obj)
And if you have a Controller action that returns json, you can wrap it in Ok result
Ok(jsonObj)
And the client will see this with the corresponding Content-Type header as "application / json"
For more information on the Json library and macros, see Docs.
source share