JCK
JCK

Reputation: 129

Karate: Match JSON Array responses where the order of array is different in each hit

I have a scenario where a portion of the response arrays is the response from a child API. child API response looks like below, but there is no specific order. And I need to check whether the child API response is present in the parent API(irrespective of the order of the elements in the child API). I followed this Karate - Match two dynamic responses thread but its not working in my case.

* def response1 =

    """
   {
    "array1": [
        {
            "element": {
                "id": "A1",
                "array11": [
                    {
                        "uid": "u123",
                        "gid": [
                            "g1"
                        ]
                    }
                ]
            }
        },
        {
            "element": {
                "id": "A2",
                "array11": [
                    {
                        "uid": "u124",
                        "gid": [
                            "g2"
                        ]
                    }
                ]
            }
        }
    ]
}
"""
* def response2 =

    """
 {
    "array1": [
        {
            "element": {
                "id": "A2",
                "array11": [
                    {
                        "uid": "u124",
                        "gid": [
                            "g2"
                        ]
                    }
                ]
            }
        },
        {
            "element": {
                "id": "A1",
                "array11": [
                    {
                        "uid": "u123",
                        "gid": [
                            "g1"
                        ]
                    }
                ]
            }
        }

    ]
}
    """

Upvotes: 2

Views: 3394

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

This is a one liner :)

* match response2.array1 contains response1.array1

Guess what, you don't have to match pure JSON all the time, using child-sections is fine.

But also read this specific part of the docs: https://github.com/intuit/karate#contains-short-cuts

And this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/graphql/graphql.feature

Upvotes: 2

Related Questions