WireMock. How to write proper JSONPath for array

Viewed 523

I have a request with body like below:

"RequestBody": {
        "numbers": ["20030013", "10010701", "10006402"]
    }

And I'm trying to catch above request with Wiremock like:

{
"request": {
    "urlPattern": "<my_url_com>",
    "method": "POST",
    "bodyPatterns": [
        {
            "matchesJsonPath": "$.RequestBody[?(@.numbers == ['10010701'])]"
        }
    ]
},
"response": {
    "status": 200,
    "jsonBody": {
        "ResponseBody": {
            ...
        }
    }
}

} Because I'm interested in only when request contains "10010701".

But it works only if numbers contain only one value. So, how can I catch this request with multiple values?

1 Answers

WireMock uses Goessener JSONPath, so we should be able to use JS inside of the ?() filter. We can then use the .indexOf() function to check if a value exists in an array. If it does, it will return the index of the item, and if it doesn't it will return -1. By checking that the value is not -1, we can assume the value exists in the array.

Info taken from here

"matchesJsonPath": "$.RequestBody[?[@.numbers.indexOf('10010701') != -1)]"
Related