1

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

1 Answer 1

9
* 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'
Sign up to request clarification or add additional context in comments.

3 Comments

This works fine, wasn't aware that even get[*] is also possible.Thank you Peter.
hi peter if I have to get cid where nationalityDecription=='USA and cidMeta"== "01" then how we will achieve this.
@AshishPatil please search for answers: stackoverflow.com/search?q=%5Bkarate%5D+filter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.