Sayed
Sayed

Reputation: 73

JSON Response validation with KARATE

I am stuck at validation of following response.

When I make a get request using karate I get the following response, I would like to validate it.

    My Response is: {
      "response": [
        {
          "tagName": "CaseTag",
          "value": "CaseTagMckAuto_TagValueOne",
          "entityType": "Case",
          "partitionId": 1,
          "appId": 1,
          "id": 46,
          "tagId": 1,
          "entityId": 1
        },
        {
          "tagName": "CaseTag",
          "value": "CaseTagMckAuto_TagValue",
          "entityType": "Case",
          "partitionId": 1,
          "appId": 1,
          "id": 45,
          "tagId": 1,
          "entityId": 1
        }
      ]
    }

I tried:

And match response[0].tagName contains ['CaseTag']


Then match each res contains
    ...
           {
              {tagName: 'CaseTag', value: 'CaseTagMckAuto_TagValueOne', 
               entityType: 'Case', partitionId: 1, appId: 1,id: 46, 
               tagId: 1, entityId:1}
           }
    ...

And match response[0] == {tagName: 'CaseTag', value: 
      'CaseTagMckAuto_TagValueOne', entityType: 'Case', partitionId: 1, 
       appId: 1,id: 46, tagId: 1, entityId:1}

All three statements fails json it is not valid json array when i try giving

And match response == {tagName: 'CaseTag', value: 
      'CaseTagMckAuto_TagValueOne', entityType: 'Case', partitionId: 1, 
       appId: 1,id: 46, tagId: 1, entityId:1}        

It says it is not String. Could you please help me to validate this respone.

Upvotes: 0

Views: 11668

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Looks like you got confused with the nesting. Here is a sample that works for me, just paste this into a feature file, no HTTP required:

* def response = 
"""
[
   {
      "tagName":"CaseTag",
      "value":"CaseTagMckAuto_TagValueOne",
      "entityType":"Case",
      "partitionId":1,
      "appId":1,
      "id":46,
      "tagId":1,
      "entityId":1
   },
   {
      "tagName":"CaseTag",
      "value":"CaseTagMckAuto_TagValue",
      "entityType":"Case",
      "partitionId":1,
      "appId":1,
      "id":45,
      "tagId":1,
      "entityId":1
   }
]
"""
* match response == '#[2]'
* match response[0].tagName == 'CaseTag'
* match each response == { tagName: 'CaseTag', value: '#string', entityType: 'Case', partitionId: 1, appId: 1, id: '#number', tagId: 1, entityId: 1 }
* match each response contains { tagName: 'CaseTag', entityType: 'Case', partitionId: 1, appId: 1, tagId: 1, entityId: 1 }

See how your JSON has a response within it. If this is really the case, just do this before using match, to make it like what I have above:

* def response = response.response

Upvotes: 6

Related Questions