You can build a JSON object tree (a JsValue
) manually
import play.api.libs.json._
val json = JsObject(Map(
"name" -> JsString("Jsony McJsonface"),
"age" -> JsNumber(18),
"hobbies" -> JsArray(Seq(
JsString("Fishing"),
JsString("Hunting"),
JsString("Camping")
))
))
Or with the shorter equivalent syntax, based on a few implicit conversions :
import play.api.libs.json._
val json = Json.obj(
"name" -> "Jsony McJsonface",
"age" -> 18,
"hobbies" -> Seq(
"Fishing",
"Hunting",
"Camping"
)
)
To get the JSON string :
json.toString
// {"name":"Jsony McJsonface","age":18,"hobbies":["Fishing","Hunting","Camping"]}
Json.prettyPrint(json)
// {
// "name" : "Jsony McJsonface",
// "age" : 18,
// "hobbies" : [ "Fishing", "Hunting", "Camping" ]
// }