Search code examples
apitestingdynamicresponsekarate

Parsing Dynamic response in Karate


I want to verify the value of "RequestedName" in the following response, where the keys for different drugs is dynamic:

{
  "requestId": "c826bee1-610e-4dee-b998-1fe4f8c15a1b",
  "requestSource": "",
  "responseSource": "client",
  "status": 200,
  "responseCodes": [
    {
      "code": "S00001",
      "message": "Success.",
      "params": {
        "entity": ""
      }
    }
  ],
  "context": null,
  "payload": {
    "0113ccf86ba79b698b8e7a8fb9effc4b": {
      "RequestedName": "paracetamol",
      "SearchKey": "0113ccf86ba79b698b8e7a8fb9effc4b",
      "Name": "Genexa Acetaminophen Extra Strength",
      "PrescribableName": "",
      "ProductCodes": [
        "69676-0059"
      ],
      "DosageForm": "Tablet, coated",
      "Route": "Oral",
      "Approved": false,
      "UnApproved": false,
      "Generic": false,
      "Allergen": false,
      "Vaccine": false,
      "Strength": {
        "Number": "500",
        "Unit": "mg/1"
      },
      "Purposes": {},
      "SideEffects": {}
    },
    "0349fa4ea29da419c46745bc7e2a6c07": {
      "RequestedName": "paracetamol",
      "SearchKey": "0349fa4ea29da419c46745bc7e2a6c07",
      "Name": "Pain Reliever",
      "PrescribableName": "",
      "ProductCodes": [
        "70677-0168"
      ],
      "DosageForm": "Tablet, extended release",
      "Route": "Oral",
      "Approved": true,
      "UnApproved": false,
      "Generic": true,
      "Allergen": false,
      "Vaccine": false,
      "Strength": {
        "Number": "650",
        "Unit": "mg/1"
      },
      "Purposes": {},
      "SideEffects": {}
    },
    "060cfbde5d82d947c56aac304c136fd3": {
      "RequestedName": "paracetamol",
      "SearchKey": "060cfbde5d82d947c56aac304c136fd3",
      "Name": "Betr Pain Relief",
      "PrescribableName": "Acetaminophen 500 mg Oral Tablet",
      "ProductCodes": [
        "80267-0484"
      ],
      "DosageForm": "Tablet",
      "Route": "Oral",
      "Approved": false,
      "UnApproved": false,
      "Generic": false,
      "Allergen": false,
      "Vaccine": false,
      "Strength": {
        "Number": "500",
        "Unit": "mg/1"
      },
      "Purposes": {},
      "SideEffects": {}
    },
    "0950fcbac262c1c1d3a9e6630615a5f9": {
      "RequestedName": "paracetamol",
      "SearchKey": "0950fcbac262c1c1d3a9e6630615a5f9",
      "Name": "Acetaminophen",

I tired this:

* def list = []
* def fun = function(k, v){ karate.appendTo('list', { key: k, val: v } )}
* karate.forEach(response, fun)
* def keys = $list[?(@.val.payload.RequestedName==drugName)].key

but not working, getting error as below:

  • def keys = $list[?(@.val.payload.RequestedName==drugName)].key Failed to parse filter: [?(@.val.payload.RequestedName==drugName)], error on position: 32, char: d testsuite/GetDrugs.feature:20

Solution

  • Here is the approach you can use:

    * def response =
    """
    {
      dynamicKey1: {
        fixedKey: 'fixedValue1',
        dataKey: 'dataValue2'
      },
      dynamicKey2: {
        fixedKey: 'fixedValue2',
        dataKey: 'dataValue2'
      }
    }
    """
    * def keys = []
    * def fun = function(k, v){ if (v.fixedKey == 'fixedValue2') keys.push(k) }
    * karate.forEach(response, fun)
    * match keys == ['dynamicKey2']