Another night on the startup, I learned something about json traversal using play json library.
Play Json api is cool, for example I can convert the json string to Json using Json.parse("json string").
The fun part is when I had to traverse the Json object. For example I want to make sure that the json response I received from my HTTP server has the proper values.
So, here is the example of traversing the given list of json objects,
Reference
https://www.playframework.com/documentation/2.5.x/ScalaJson
Play Json api is cool, for example I can convert the json string to Json using Json.parse("json string").
The fun part is when I had to traverse the Json object. For example I want to make sure that the json response I received from my HTTP server has the proper values.
So, here is the example of traversing the given list of json objects,
scala> import play.api.libs.functional.syntax._
import play.api.libs.functional.syntax._
scala> import play.api.libs.json._
import play.api.libs.json._
scala> implicit val productReader = (
| (__ \ "id").read[Long] and
| (__ \ "name").read[String] and
| (__ \ "brand").read[String] and
| (__ \ "price").read[Double] and
| (__ \ "category").read[String]and
| (__ \ "marketplace").read[String] and
| (__ \ "date").read[Long]
| ) tupled;
warning: there were 1 feature warning(s); re-run with -feature for details
productReader: play.api.libs.json.Reads[(Long, String, String, Double, String, String, Long)] = play.api.libs.json.Reads$$anon$8@711cbc4c
scala> val x = """[{"0":{"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1}}, {"1":{"price":200.0,"releasedOn":1419783519,"brand":"Steven Wilson","category":"Metal","name":"Hands.Can not. Erase","marketplaceName":"Kscope","id":2}}, {"2":{"price":200.0,"releasedOn":1419783519,"brand":"sleepmakeswaves","category":"Rock","name":"And So We Destroyed Everything","marketplaceName":"Bird's Robe Records","id":3}}]"""
x: String = [{"0":{"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1}}, {"1":{"price":200.0,"releasedOn":1419783519,"brand":"Steven Wilson","category":"Metal","name":"Hands.Can not. Erase","marketplaceName":"Kscope","id":2}}, {"2":{"price":200.0,"releasedOn":1419783519,"brand":"sleepmakeswaves","category":"Rock","name":"And So We Destroyed Everything","marketplaceName":"Bird's Robe Records","id":3}}]
scala> val jsonArray = Json.parse(x).as[JsArray]
jsonArray: play.api.libs.json.JsArray = [{"0":{"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1}},{"1":{"price":200.0,"releasedOn":1419783519,"brand":"Steven Wilson","category":"Metal","name":"Hands.Can not. Erase","marketplaceName":"Kscope","id":2}},{"2":{"price":200.0,"releasedOn":1419783519,"brand":"sleepmakeswaves","category":"Rock","name":"And So We Destroyed Everything","marketplaceName":"Bird's Robe Records","id":3}}]
scala> jsonArray \\ "0"
res14: Seq[play.api.libs.json.JsValue] = ListBuffer({"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1})
scala> jsonArray(0)
res3: play.api.libs.json.JsValue = {"0":{"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1}}
scala> jsonArray(0).\("0")
res4: play.api.libs.json.JsValue = {"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1}
scala> jsonArray(0).\\("0")
res5: Seq[play.api.libs.json.JsValue] = List({"price":100.0,"releasedOn":1419783511,"brand":"Palace Skate Boards","category":"Men","name":"Tops","marketplaceName":"Oodni boutique","id":1})
scala> jsonArray(0).\("0").\("price")
res11: play.api.libs.json.JsValue = 100.0
scala> jsonArray(0).\("0").\("name")
res12: play.api.libs.json.JsValue = "Tops"
scala> jsonArray(0).\("0").\("brand")
res13: play.api.libs.json.JsValue = "Palace Skate Boards"
scala> ((jsonArray \\ "0").map(on => on \ "name").map(_.as[String])).asInstanceOf[Seq[String]](0)
res33: String = Tops
Reference
https://www.playframework.com/documentation/2.5.x/ScalaJson
References