Serializing a Scala List to JSON in Play2
scala> import play.api.libs.json._
import play.api.libs.json._
scala> case class User(name: String, age: Int)
defined class User
scala> implicit val userFormat = Json.format[User]
userFormat: play.api.libs.json.OFormat[User] = play.api.libs.json.OFormat$$anon$1@38d2c662
scala> val userList = List(User("Example 1", 20), User("Example 2", 42))
userList: List[User] = List(User(Example 1,20), User(Example 2,42))
scala> val users = Json.obj("users" -> userList)
users: play.api.libs.json.JsObject = {"users":[{"name":"Example 1","age":20},{"name":"Example 2","age":42}]}
Comments
-
Johan Paul almost 2 years
I am trying to deserialize a list of Scala objects to a JSON map in Play2 - a pretty trivial use case with JSON, I'd say. My JSON output would be something along the lines of:
{ "users": [ { "name": "Example 1", "age": 20 }, { "name": "Example 2", "age": 42 } ] }
To achieve this I am looking at the Play2's JSON documentation titled "The Play JSON library". To me their examples are pretty trivial, and I've confirmed that they work for me. Hence, I am able to deserialize a single
User
object properly.But making a map containing a list in JSON seems a bit verbose in Play2, when I read the documentation. Is there something I am not grokking?
This is basically my simple Scala code:
case class User(name: String, age: Int) object UserList { implicit val userFormat = Json.format[User] val userList = List(User("Example 1", 20), User("Example 2", 42)) val oneUser = Json.toJson(userList(0)) // Deserialize one Scala object properly to JSON. // JSON: { "user" : [ <-- put content of userList here. How? // ] // } }
So my question would be; how can I transform the content of the
userList
List above to a hash in the JSON in a more generic way than explicitly writing out each hash element, as the Play documentation suggests? -
Johan Paul almost 11 yearsOh wow - that was simple! I have to read the docs again. Wonder why this hasn't been told more clearly - or I missed it. Thanks!!
-
Johan Paul almost 11 yearsI must say, that just by reading the docs at www.playframework.com about Play's JSON support, I would have never came up with how to achieve what I wanted - and what you pasted. The docs seem to be quite rubbish.
-
user5880801 almost 11 years@JohanPaul yeah, at some point you have to look at the method signatures and read the source code (to see what instances of the
Writes
typeclass are already defined by Play, in this particular case). -
angelokh about 10 years@IonuțG.Stan How did you import all play jars to scala console? I tried to reproduce your commands but I can't.
-
user5880801 about 10 years@angelokh I started the scala console from inside an sbt session in a Play project directory I had locally. I've described an alternative in this blog post igstan.ro/posts/…. Hope it helps.
-
Ilan Biala almost 10 yearsCan someone explain what each line does? I'm fairly new to Scala so I'm not sure how some of the code is working.
-
Zee almost 8 yearsWay better than the play documentation. They should pay you.
-
Kevin Meredith over 7 yearsFor this example alone, it's sufficient to use
implicit val userReads = Json.writes[User]
rather thanJson.format...
.