Accepted answer

There are many ways to do this with the Play JSON Library. The main difference is the usage of Scala case class or not.

Given a simple json

val json = Json.parse("""{"people": [ {"name":"Jack", "age": 19}, {"name": "Tony", "age": 26} ] }""")

You can use case class and Json Macro to automatically parse the data

import play.api.libs.json._

case class People(name: String, age: Int)

implicit val peopleReader = Json.reads[People]
val peoples = (json \ "people").as[List[People]]

Or without case class, manually

import play.api.libs.json._
import play.api.libs.functional.syntax._

implicit val personReader: Reads[(String, Int)] = (
  (__ \ "name").read[String] and 
  (__ \ "age").read[Int]
val peoples = (json \ "people").as[List[(String, Int)]]

In other words, check the very complete documentation on this subject :)


If you don't have the object type or don't want to write a Reads, you can use .as[Array[JsValue]]

val jsValue = Json.parse(text)
val list = (jsValue \ "people").as[Array[JsValue]]


list.foreach(a => println((a \ "name").as[String]))

In the older version (2.6.x) it is possible to use .as[List[JsValue]] but newer versions only support Array.

Related Query

More Query from same tag