nagpai
nagpai

Reputation: 169

Extract data from json array in Karate

In the below JSON response, I need to extract the 'cid' for the record that has the 'nationalityDecription' as 'USA'. By using this query as a reference, I used the below loc in the karate feature file, but 1st line itself fails with syntax error(tried different combinations). For now, I'm using the custom javascript as a workaround which is working fine. I need help to check if i'm missing anything in syntax. Thanks

Response:

{
    "header": {
        "Id": "12345678",
        "timeStamp": "2018-09-17T10:09:812.000"
    },
    "dataRecords": [
        {
            "cid": "31H678",
            "cidMeta": "00",
            "nationalityDecription": "CHINA"
        },
        {
            "cid": "31S421",
            "cidMeta": "01",
            "nationalityDecription": "USA"
        }
    ]
} 

Feature file:

* def record= $response.dataRecords[?(@.nationalityDecription=='USA')] 
* def cid = record.cid

Upvotes: 1

Views: 9814

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

* def response = { "header": { "Id": "12345678", "timeStamp": "2018-09-17T10:09:812.000" }, "dataRecords": [ { "cid": "31H678", "cidMeta": "00", "nationalityDecription": "CHINA" }, { "cid": "31S421", "cidMeta": "01", "nationalityDecription": "USA" } ] }
* def cid = get[0] response.dataRecords[?(@.nationalityDecription=='USA')].cid
* match cid == '31S421'

Upvotes: 9

Related Questions