Issue: JsonPathMatcher - not working for rootless jsons? #71

Closed
opened 2025-12-29 14:21:59 +01:00 by adam · 5 comments
Owner

Originally created by @FSatmar on GitHub (Mar 1, 2018).

Originally assigned to: @StefH on GitHub.

Hi,
Could you pls help me with defining a JsonPathMatcher for a json of the type:

{
   id: 1,
   name: "AwesomeName"
}

I have tried "$.[?(@.id == 1)]" but it doesn't seem to work.

I tried it with the following test as well:

            // given
            var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.[?(@.id == 1)]"));

            // when
            string bodyAsString = "{  \"id\": 1, \"name\": \"AwesomeName\"  }";
            byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
            var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);

            // then
            var requestMatchResult = new RequestMatchResult();
            Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
Originally created by @FSatmar on GitHub (Mar 1, 2018). Originally assigned to: @StefH on GitHub. Hi, Could you pls help me with defining a JsonPathMatcher for a json of the type: ``` { id: 1, name: "AwesomeName" } ``` I have tried "$.[?(@.id == 1)]" but it doesn't seem to work. I tried it with the following test as well: ``` // given var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.[?(@.id == 1)]")); // when string bodyAsString = "{ \"id\": 1, \"name\": \"AwesomeName\" }"; byte[] body = Encoding.UTF8.GetBytes(bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); // then var requestMatchResult = new RequestMatchResult(); Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0); ```
adam added the question label 2025-12-29 14:21:59 +01:00
adam closed this issue 2025-12-29 14:22:00 +01:00
Author
Owner

@StefH commented on GitHub (Mar 2, 2018):

When testing at http://jsonpath.herokuapp.com/

This works...
json

So actually, I don't know what's wrong?

@StefH commented on GitHub (Mar 2, 2018): When testing at http://jsonpath.herokuapp.com/ This works... ![json](https://user-images.githubusercontent.com/249938/36897858-95f904ce-1e18-11e8-8bcb-e5a32ead5b24.png) So actually, I don't know what's wrong?
Author
Owner

@FSatmar commented on GitHub (Mar 2, 2018):

I tried it like this:


        [Test]
        public void Request_WithBodyJsonPathMatcher_true()
        {
            // given
            var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.[?(@.Id == 1)]"));

            var requestData = new JsonObject()
            {
                Id = 1,
                Name = "AwesomeName"
            };

            var bodyData = new BodyData {BodyAsJson = requestData};

            // when
            var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", "::1", bodyData);

            // then
            var requestMatchResult = new RequestMatchResult();
            spec.GetMatchingScore(request, requestMatchResult).Should().Be(1);
        }

        [DataContract]
        public sealed class JsonObject
        {
            [DataMember(Order = 1)]
            public int Id { get; set; }
            [DataMember(Order = 2)]
            public string Name { get; set; }
        }

But still no luck.
Also tried it against a real mapping with a real request using Postman & JaSon and only got the "No matching found" response.

@FSatmar commented on GitHub (Mar 2, 2018): I tried it like this: ``` [Test] public void Request_WithBodyJsonPathMatcher_true() { // given var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.[?(@.Id == 1)]")); var requestData = new JsonObject() { Id = 1, Name = "AwesomeName" }; var bodyData = new BodyData {BodyAsJson = requestData}; // when var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", "::1", bodyData); // then var requestMatchResult = new RequestMatchResult(); spec.GetMatchingScore(request, requestMatchResult).Should().Be(1); } [DataContract] public sealed class JsonObject { [DataMember(Order = 1)] public int Id { get; set; } [DataMember(Order = 2)] public string Name { get; set; } } ``` But still no luck. Also tried it against a real mapping with a real request using Postman & JaSon and only got the "No matching found" response.
Author
Owner

@StefH commented on GitHub (Mar 2, 2018):

For some reason:

It should be "$..[?(@.Id == 1)]".
See linked unit-test.---> 2b8a58c68c

Maybe a bit related to https://github.com/JamesNK/Newtonsoft.Json/issues/1256 ?

@StefH commented on GitHub (Mar 2, 2018): For some reason: It should be `"$..[?(@.Id == 1)]"`. See linked unit-test.---> https://github.com/WireMock-Net/WireMock.Net/commit/2b8a58c68ce53e94070e61f08e28ca0a354f3686 Maybe a bit related to https://github.com/JamesNK/Newtonsoft.Json/issues/1256 ?
Author
Owner

@StefH commented on GitHub (Mar 24, 2018):

@FSatmar This is now fixed, please check new NuGet 1.0.3.12

@StefH commented on GitHub (Mar 24, 2018): @FSatmar This is now fixed, please check new NuGet 1.0.3.12
Author
Owner

@FSatmar commented on GitHub (Mar 26, 2018):

Great, it's working with the double dots, thank you very much!

@FSatmar commented on GitHub (Mar 26, 2018): Great, it's working with the double dots, thank you very much!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#71