Search code examples
groovyrest-assuredrest-assured-jsonpath

REST Assured getting map values only from response body


Imagine i called a RESTful service and it returns this in response body

[
  {
    "key": "Reason1 key",
    "values": {
      "en": "Reason1 English translation",
      "es": "Reason1 Spanish translation"
    }
  },
  {
    "key": "Reason2 key",
    "values": {
      "en": "Reason2 English translation",
      "es": "Reason2 Spanish translation"
    }
  }
]

I need to get only values which map key is 'en'

So i am expecting the result to be like that after filtering it

["Reason1 English translation", "Reason2 English translation"]

OR

{"Reason1 English translation", "Reason2 English translation"}

I had try this next code but still not completed

response.then().body("findAll { it }.collect { it.values }", hasItems("Reason1 English translation", "Reason2 English translation"))

and it return

[{en=Reason1 English translation, es=Reason1 Spanish translation}, {en=Reason2 English translation, es=Reason2 Spanish translation}]

So how can i get only map values which matching map key condition of equality of 'en'?


Solution

  • Changes to ....body("findAll { it }.collect { it.values.en }"

    Add en to get the en nodes rather than the entire values node.