Roman
Roman

Reputation: 91

RestAssured JsonPath: Issue with getting list from json

Can someone who familiar with jsonPath give me an advice how can i get a list of title from each item0, item1, item2 etc.

This query will return

findAll {it.key.startsWith('item')}

List of maps where key is item and value is body of item object and i don't know how to get List of titles

{
   "jcr:primaryType":"nt:unstructured",
   "item0":{
      "jcr:primaryType":"nt:unstructured",
      "tabType":"regular",
      "uniqueId":927,
      "hide":"no",
      "title":"title 0",
      "locales":[
         "Locale:en_us",
         "Locale:fr_ca",
         "Locale:es",
         "Locale:pt"
      ],
      "cq:tags":[
         "tag0"
      ]
   },
   "item1":{
      "jcr:primaryType":"nt:unstructured",
      "tabType":"regular",
      "uniqueId":445,
      "hide":"no",
      "title":"title 1",
      "locales":[
         "Locale:en_us",
         "Locale:fr_ca",
         "Locale:pt",
         "Locale:es"
      ],
      "cq:tags":[
         "Tag1"
      ]
   }

Upvotes: 1

Views: 988

Answers (2)

Roman
Roman

Reputation: 91

Finally found a solution for my question.

.items*.find {it.key.startsWith('item')}.value 

Upvotes: 0

Johan
Johan

Reputation: 40500

The syntax is a bit awkward but here's one way to do it:

findAll {it.key.startsWith('item')}*.getValue().title

Explanation:

First we find all entries whose keys starts with "item". For each entry we get its value (using the spread operator) and then get title.

Upvotes: 2

Related Questions