Home > Back-end >  How to get the All index values in Groovy JSON xpath
How to get the All index values in Groovy JSON xpath

Time:11-12

Please find the attached Groovy code which I am using to get the particular filed from the response body.

Query 1 :

It is retrieving the results when the I am using the correct Index value like if the data.RenewalDetails[o], will give output as Value 1 and if the data.RenewalDetails[1], output as Value 2.

But in my real case, I will never know about number of blocks in the response, so I want to get all the values that are satisficing the condition, I tried data.RenewalDetails[*] but it is not working. Can you please help ?

Query 2:

Apart from the above condition, I want to add one more filter, where "FamilyCode": "PREMIUM" in the Itemdetails, Can you help on the same ?

def BoundId = new groovy.json.JsonSlurper().parseText('{"data":{"RenewalDetails":[{"ExpiryDetails":{"duration":"xxxxx","destination":"LHR","from":"AUH","value":2,"segments":[{"valudeid":"xxx-xx6262-xxxyyy-1111-11-11-1111"}]},"Itemdetails":[{"BoundId":"Value1","isexpired":true,"FamilyCode":"PREMIUM","availabilityDetails":[{"travelID":"AAA-AB1234-AAABBB-2022-11-10-1111","quota":"X","scale":"XXX","class":"X"}]}]},{"ExpiryDetails":{"duration":"xxxxx","destination":"LHR","from":"AUH","value":2,"segments":[{"valudeid":"xxx-xx6262-xxxyyy-1111-11-11-1111"}]},"Itemdetails":[{"BoundId":"Value2","isexpired":true,"FamilyCode":"PREMIUM","availabilityDetails":[{"travelID":"AAA-AB1234-AAABBB-2022-11-10-1111","quota":"X","scale":"XXX","class":"X"}]}]}]},"warnings":[{"code":"xxxx","detail":"xxxxxxxx","title":"xxxxxxxx"}]}')
        .data.RenewalDetails[0].Itemdetails.find { itemDetail ->
    itemDetail.availabilityDetails[0].travelID.length() == 33
}?.BoundId

println "Hello "   BoundId

CodePudding user response:

Something like this:

def txt = '''\
  {
    "data": {
        "RenewalDetails": [
            {
                "ExpiryDetails": {
                    "duration": "xxxxx",
                    "destination": "LHR",
                    "from": "AUH",
                    "value": 2,
                    "segments": [
                        {
                            "valudeid": "xxx-xx6262-xxxyyy-1111-11-11-1111"
                        }
                    ]
                },
                "Itemdetails": [
                    {
                        "BoundId": "Value1",
                        "isexpired": true,
                        "FamilyCode": "PREMIUM",
                        "availabilityDetails": [
                            {
                                "travelID": "AAA-AB1234-AAABBB-2022-11-10-1111",
                                "quota": "X",
                                "scale": "XXX",
                                "class": "X"
                            }
                        ]
                    }
                ]
            },
            {
                "ExpiryDetails": {
                    "duration": "xxxxx",
                    "destination": "LHR",
                    "from": "AUH",
                    "value": 2,
                    "segments": [
                        {
                            "valudeid": "xxx-xx6262-xxxyyy-1111-11-11-1111"
                        }
                    ]
                },
                "Itemdetails": [
                    {
                        "BoundId": "Value2",
                        "isexpired": true,
                        "FamilyCode": "PREMIUM",
                        "availabilityDetails": [
                            {
                                "travelID": "AAA-AB1234-AAABBB-2022-11-10-1111",
                                "quota": "X",
                                "scale": "XXX",
                                "class": "X"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "warnings": [
        {
            "code": "xxxx",
            "detail": "xxxxxxxx",
            "title": "xxxxxxxx"
        }
    ]
}'''

def json = new groovy.json.JsonSlurper().parseText txt

List<String> BoundIds = json.data.RenewalDetails.Itemdetails*.find { itemDetail ->
  itemDetail.availabilityDetails[0].travelID.size() == 33 && itemDetail.FamilyCode == 'PREMIUM'
}?.BoundId


assert BoundIds.toString() == '[Value1, Value2]'

Note, that you will get the BoundIds as a List

CodePudding user response:

  1. If you amend your code like this:

    def json = new groovy.json.JsonSlurper().parse(prev.getResponseData()
    

    you would be able to access the number of returned items as:

    def size = json.data.RenewalDetails.size()
    

    as RenewalDetails represents a List

  2. Just add as many queries you want using Groovy's && operator:

     find { itemDetail ->
         itemDetail.availabilityDetails[0].travelID.length() == 33 &&
         itemDetail.FamilyCode.equals('PREMIUM')
     }
    

More information:

  • Related