Filter with nested structure

281 views
Skip to first unread message

Deve Lopper

unread,
Jul 29, 2019, 3:34:08 AM7/29/19
to spray.io User List
Hello all,

given this json does anybody know how to filter out book that cost higher than 10 for exemple?

val json = """
{ "store": {
    "book": [
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": {
                  "value":8.95
                   "VTA": 0.05
                }
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": {
                  "value":18.95
                   "VTA": 0.05
                },
"isbn": "0-553-21311-3" } ], "bicycle": { "color": "red", "price": 19.95 } } }""".parseJson


Age Mooij

unread,
Jul 29, 2019, 3:38:36 AM7/29/19
to spray.io User List
The result of parsing the json will be a JsValue, a JsObject in this case. If you unmarshal the Json into a case class (using convertTo and JsonFormats) you will get just that. In both cases collections are implemented using standard Scala Seqs so you can use filter, map, and any other collection operator on them to manipulate the data however you want.

Hope this helps,
Age
Reply all
Reply to author
Forward
0 new messages