JsonPathMatcher filter does not work with non array JSON #370

Closed
opened 2025-12-29 15:22:08 +01:00 by adam · 4 comments
Owner

Originally created by @bettyybei on GitHub (Oct 16, 2021).

Describe the bug

JsonPathMatcher does not allow the usage of filters with non array JSON.
In the example provided in the wiki, the first matching example listed { "things": { "name": "RequiredThing" } } does not actually request match with the body matcher pattern "$.things[?(@.name == 'RequiredThing')]".

Expected behavior:

JsonPathMatcher filters should work with non array JSON

Test to reproduce

  • Set body matcher pattern to "$.things[?(@.name == 'RequiredThing')]"
  • Send request with JSON body { "things": { "name": "RequiredThing" } }

Expected behavior: Returns request match
Actual behavior: Does not match

  • This behavior may have been broken in https://github.com/WireMock-Net/WireMock.Net/pull/311
  • The other example in the wiki { "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] } works correctly because { "name": "RequiredThing" } is in an array.
  • I believe outside of wiremock, JSONPath filters are actually only supposed to be used with arrays, so this may not actually be a bug. If so, the wiki should be updated. However, I believe there is still value in allowing the ability to use filters for non-array JSON matches, otherwise there does not seem to be a way to request match multiple JSON properties without doing an exact match of all properties using JsonMatcher. (I believe JsonPartialMatcher only allows you to match 1 property, but let me know if I'm wrong about this).
Originally created by @bettyybei on GitHub (Oct 16, 2021). ### Describe the bug `JsonPathMatcher` does not allow the usage of filters with non array JSON. In the example provided in the [wiki](https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPathMatcher), the first matching example listed `{ "things": { "name": "RequiredThing" } }` does not actually request match with the body matcher pattern `"$.things[?(@.name == 'RequiredThing')]"`. ### Expected behavior: `JsonPathMatcher` filters should work with non array JSON ### Test to reproduce - Set body matcher pattern to `"$.things[?(@.name == 'RequiredThing')]"` - Send request with JSON body `{ "things": { "name": "RequiredThing" } }` Expected behavior: Returns request match Actual behavior: Does not match ### Other related info - This behavior may have been broken in https://github.com/WireMock-Net/WireMock.Net/pull/311 - The other example in the wiki `{ "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] }` works correctly because `{ "name": "RequiredThing" }` is in an array. - I believe outside of wiremock, JSONPath filters are actually only supposed to be used with arrays, so this may not actually be a bug. If so, the wiki should be updated. However, I believe there is still value in allowing the ability to use filters for non-array JSON matches, otherwise there does not seem to be a way to request match _multiple_ JSON properties without doing an exact match of all properties using `JsonMatcher`. (I believe `JsonPartialMatcher` only allows you to match 1 property, but let me know if I'm wrong about this).
adam added the question label 2025-12-29 15:22:08 +01:00
adam closed this issue 2025-12-29 15:22:08 +01:00
Author
Owner

@StefH commented on GitHub (Oct 23, 2021):

Hello @bettyybei, thank you for this issue.

I did some research, and I think that the JsonPathMatcher works fine, but the examples are wrong.

The following code works fine

var things = JObject.Parse("{ \"things\": { \"name\": \"RequiredThing\" } }");
JToken wiremockOK = things.SelectToken("[?($.things.name == 'RequiredThing')]");
wiremockOK.Dump();

image

Does this make sense?

@StefH commented on GitHub (Oct 23, 2021): Hello @bettyybei, thank you for this issue. I did some research, and I think that the JsonPathMatcher works fine, but the examples are wrong. The following code works fine ``` c# var things = JObject.Parse("{ \"things\": { \"name\": \"RequiredThing\" } }"); JToken wiremockOK = things.SelectToken("[?($.things.name == 'RequiredThing')]"); wiremockOK.Dump(); ``` ![image](https://user-images.githubusercontent.com/249938/138548015-9ca961fb-658f-4f42-bac1-74066e9baaa8.png) Does this make sense?
Author
Owner

@StefH commented on GitHub (Feb 20, 2022):

@bettyybei
Did you have time to verify this? Else I will close this question.

@StefH commented on GitHub (Feb 20, 2022): @bettyybei Did you have time to verify this? Else I will close this question.
Author
Owner

@StefH commented on GitHub (May 3, 2022):

@bettyybei
Did you have time to verify this? Else I will close this question.

@StefH commented on GitHub (May 3, 2022): @bettyybei Did you have time to verify this? Else I will close this question.
Author
Owner

@StefH commented on GitHub (Aug 25, 2022):

Hello @bettyybei,

I'll close this issue for now, in case you still have questions, please reply to this comment.

@StefH commented on GitHub (Aug 25, 2022): Hello @bettyybei, I'll close this issue for now, in case you still have questions, please reply to this comment.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#370