mardi 30 juillet 2019

Karate json path filter not working when trying to extract elements that meet some filter criteria in API response

I am trying to filter my API response using JSON Path filter using Karate framework to extract a specific value that meets one of the condition using a value from a variable but I am not able to map variable properly, so my filter not working properly. I looked at the documentation and tried multiple ways but couldn't resolve this issue.

Sample response JSON:

  "requestId": "8ehd6-6c32-4766-a253-1c9cdnidc4cec2",
  "searchRefKey": "826879461625",
  "responseStatus": {
    "code": "1",
    "reasonCode": "SUCCESS",
    "message": "Successful Request"
  },
  "passengers": [
    {
      "numOfPax": 5,
      "type": "ADT"
    }
  ],
  "slices": [
    {
      "id": 7591164138534052,
      "duration": {
        "value": 1675,
        "unit": "MINUTE"
      },
      "segments": [
        {
          "id": 1,
          "segmentRefId": 7591164138529651
        },
        {
          "id": 2,
          "segmentRefId": 7591164138531002
        },
        {
          "id": 3,
          "segmentRefId": 7591164138532394
        }
      ]
    }
  ],
  "segments": [
    {
      "id": 23783268357325705,
      "departDateTime": "2019-10-05T19:50:00",
      "arrivalDateTime": "2019-10-06T14:25:00",
      "originAirport": "LAX",
      "destinationAirport": "LHR",
      "duration": {
        "value": 635,
        "unit": "MINUTE"
      },
      "marketingAirline": "BA",
      "operatingAirline": "AA",
      "flightNumber": "1509",
      "equipmentCode": "77W",
      "subjectToGovtApproval": false,
      "numOfStops": 0,
      "stops": []
    }
  ],
  "brands": [],
  "baggageAllowances": [
  ],
  "restrictions": [
  ],
  "trips": []
}```

I am using the below script where I am using variable 'originRefId':

* def originRefId = response.slices[0].segments[0].segmentRefId
* def origin = karate.jsonPath(response, "$.segments[?(@.id=='originRefId')]")
* print 'the value of Origin is:', origin


Expected results  LAX but I am getting an empty array.
17:42:15.119 [main] INFO  com.intuit.karate - [print] the value of Origin is: [
]

Aucun commentaire:

Enregistrer un commentaire