Stanislav Verjikovskiy
Stanislav Verjikovskiy

Reputation: 343

Parse Json array response in scala\Play

There is a web service returning array of something

{"apps": [{"name": "one"}, {"name": "two"}]}

In my code I want to iterate every name

val request = WS.url(s"http://localhost:9000/getData")

val json = request.get.map { response =>
  (response.json \ "apps" \\ "name")
}

json.foreach(println)

However all my attempts return single record

// Expect
one
two
// Actual
ListBuffer("one", "two")

Upvotes: 0

Views: 1510

Answers (2)

Feyyaz
Feyyaz

Reputation: 3216

First of all, the neat solution here would be:

val request = WS.url(s"http://localhost:9000/getData")

request.get.map { response =>
  val names = (response.json \ "apps" \\ "name")
  names.foreach(println)
}

Secondly, if you don't want to get confused about the types, you should change your naming standards. For a Future object, you may start with the prefix future, for an Option, it could start with maybe, etc. If you do so, the problem in your example will be more obvious:

val request = WS.url(s"http://localhost:9000/getData")

val futureJson = request.get.map { response =>
  (response.json \ "apps" \\ "name")
}

futureJson.foreach(println) // you call foreach for a Future, not for a List

Thirdly, why would Future trait have a method called foreach? I think that's confusing for beginners and even mid-level developers. We know from other languages that, foreach means iterate over a list of objects. In Scala, it is considered part of "Monadic operations" which is still a gray area for me :), but the comment for Future.foreach in Scala source is this:

  /** Asynchronously processes the value in the future once the value becomes available.
   *
   *  Will not be called if the future fails.
   */
  def foreach[U]

Upvotes: 1

josephpconley
josephpconley

Reputation: 1723

Your value of json is actually a Future[Seq[JsValue]], so when you foreach over the future you get the entire list back. You would need an additional foreach to iterate over the list of values.

Upvotes: 1

Related Questions