Reputation: 1202
I having big toruble with Argonaut. I am needing to collect all elements in JSON array. For example, I having this data in JSON.
val data = """{"id": 1, "items": [{"name": "foo","price": 10},{"name": "bar","price": 20}]}"""
Then I am needing to collect all name
values into List. So I am getting this
List("foo", "bar")
This is meaning that I am needing to traverse array so I am choosing Argonaut library to do this. But is very difficult to know how API is working in Argonaut. So far I have this,
val data = """{"id": 1, "items": [{"name": "foo","price": 10},{"name": "bar","price": 20}]}""".parseOption
data flatMap (k =>
+k --\ "items" flatMap (_.downArray) map (- _)
)
But I am not sure how to get values. Please I am needing advices here.
Upvotes: 4
Views: 102
Reputation:
If you add argonaut-monocle you can do this easily as follows:
import argonaut._
import Argonaut._
import argonaut.JsonPath._
scala> val json: Option[Json] = """{"id": 1, "items": [{"name": "foo","price": 10},{"name": "bar","price": 20}]}""".parseOption
json: Option[argonaut.Json] = Some({"id":1,"items":[{"name":"foo","price":10},{"name":"bar","price":20}]})
scala> root.items.each.name.string.getAll(json.get)
res1: List[String] = List(foo, bar)
Upvotes: 4