Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match JSON in wiremock which has values same for fields but may not be in same order

I have request JSON body [ {"name" : "Ram"}, {"name" : "Sam"} ]

and this is the input for wiremock request I need to match the request even if the JSON has same content but values may not be in same order. Example, [ {"name" : "Sam"}, {"name" : "Ram"} ]

The method which I use is .withRequestBody. I tried withequalToJson` but does not work. What is that matcher which checks only JSON contents and not order ?

like image 276
user9920500 Avatar asked Oct 29 '25 05:10

user9920500


1 Answers

This can be resolved using JsonPath that is part of the bodyPatterns equality matching functionality.

{
  "request" : {
    "urlPathPattern" : "/jpath/.*",
    "method" : "GET",
     "bodyPatterns" : [ {
      "matchesJsonPath" : "$[?(@.name == 'Sam')]"
    } ]   
  },
  "response" : {
    "status" : 200,
    "body" : "Works"
  }
}

Using a JsonPath online evaluator it is easy to test JsonPath expressions. For more details on what is possible have a look here.

like image 83
A. Kootstra Avatar answered Oct 30 '25 18:10

A. Kootstra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!