Tuesday 13 December 2022

How do I ignore JSON array order during request matching?

When matching JSON requests, sometimes the array order is unpredictable e.g. if the array is used to represent a set of unordered objects.

{
  "exampleId": 1
  "exampleRoot": {
    "exampleArray": [
      {
        "id": 1
      },
      {
        "id": 2
      }
    ]
  }
}

To match this type of request in Traffic Parrot, there are several options, here are some of them:

  • matchesJson matcher with {{anyElements}} wildcard token
    {
      "exampleId": 1,
      "exampleRoot": {
        "exampleArray": "{{anyElements}}"
      }
    }
  • matchesJsonPath matcher which uses standard JSONPath notation
    $[?(@.exampleId == 1 && '1' in @.exampleRoot.exampleArray[*].id && '2' in @.exampleRoot.exampleArray[*].id)]

You can find more examples of advanced matchers in the Traffic Parrot documentation

No comments:

Post a Comment