-1

I'm facing difficulty in extracting the values using JsonPath. Here is my json,

    applications:[
    {
    "ab": 123,
    "isReady": true,
    "proId": 234
    },
    {
    "ab": 345,
    "isReady": false,
    "proId": 098
    },
    {
    "ab": 332,
    "isReady": true,
    "proId": 100
    }
]

I wanted to extract values of "ab" and "proId" when "isReady" value is true. And once it is done, I need to construct the below json with above mentioned values. Example below,

[
{  
   "ab": 332,
   "proId": 100
},
{
    "ab": 123,
    "proId": 234
}
]

I can only be able to extract single values like below,

List<Integer> abList = jsonFindAllUserJobApplicantsResponse.get("applications.findAll{ it.isReady == true}.ab");

List<Integer> proIdList = jsonFindAllUserJobApplicantsResponse.get("applications.findAll{ it.isReady == true}.proId");

Could you please help in this scenario on how to extract and construct the desired result?

Thanks a lot for your help.

Shr4N
  • 435
  • 3
  • 18
  • [Please provide minimal completed verifiable example](https://stackoverflow.com/help/mcve) – Emre Savcı Aug 13 '18 at 12:20
  • Possible duplicate of [Rest-assured. Is it possible to extract value from request json?](https://stackoverflow.com/questions/21166137/rest-assured-is-it-possible-to-extract-value-from-request-json) – Emre Savcı Aug 13 '18 at 12:22
  • @Emre I'm sorry Emre but the question is different, my requirement is to extract two key values at the same time based on condition of another key present in then object. – Shr4N Aug 13 '18 at 13:18

1 Answers1

0

try this:

    ArrayList<Map<String,?>> result = jsonFindAllUserJobApplicantsResponse.get("applications.findAll{ it.isReady == true}");
    try {
        // use Jackson library to convert map to json string
        new ObjectMapper().writeValueAsString(result);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

reference: https://james-willett.com/2017/05/rest-assured-gpath-json/

Thomas.L
  • 321
  • 1
  • 6