How to pass attributes in JsonPath #375

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

Originally created by @AndresGMD on GitHub (Oct 20, 2021).

Hello.

In this example I would like to get ID using Cleaning Path

I've defined my path like /path/* And I use the attribute request.PathSegments.1 to get the (*) value

How to pass this attribute in JsonPAth like this?

      "BodyAsJson": {
        "method": "{{request.method}}",
        "response": "{{JsonPath.SelectToken request.body \"$.person[?(@.id == {{request.PathSegments.1 }} )]\"}}"
      },

Thanks in Advance!

Originally created by @AndresGMD on GitHub (Oct 20, 2021). Hello. In this example I would like to get ID using Cleaning Path I've defined my path like /path/* And I use the attribute request.PathSegments.1 to get the (*) value How to pass this attribute in JsonPAth like this? ``` json "BodyAsJson": { "method": "{{request.method}}", "response": "{{JsonPath.SelectToken request.body \"$.person[?(@.id == {{request.PathSegments.1 }} )]\"}}" }, ``` Thanks in Advance!
adam added the question label 2025-12-29 15:22:12 +01:00
adam closed this issue 2025-12-29 15:22:12 +01:00
Author
Owner

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

Hello @andresendava ,

Did you try request.PathSegments.2 ?

@StefH commented on GitHub (Oct 20, 2021): Hello @andresendava , Did you try `request.PathSegments.2` ?
Author
Owner

@AndresGMD commented on GitHub (Oct 20, 2021):

Yes

@AndresGMD commented on GitHub (Oct 20, 2021): Yes
Author
Owner

@AndresGMD commented on GitHub (Oct 20, 2021):

I know that is attribute splitting path and I would like to use a clean path to pass parameters.

@AndresGMD commented on GitHub (Oct 20, 2021): I know that is attribute splitting path and I would like to use a clean path to pass parameters.
Author
Owner

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

Sorry, I don't understand what you want. Can you please provide a full example what you're trying to achieve?

@StefH commented on GitHub (Oct 20, 2021): Sorry, I don't understand what you want. Can you please provide a full example what you're trying to achieve?
Author
Owner

@AndresGMD commented on GitHub (Oct 20, 2021):

Off course!!

I try to implemented a Get By ID using a clean Path

For example. In the request of my static file configuration I define the next Wildcardmatcher

"Request": {
      "Path": {
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "/json/*",
            "IgnoreCase": false
          }
        ]
      },
      "Methods": [
        "PUT"
      ]

I use PUT because I don know how to use Filters when I use BodyAsFile Instead (Issue #658) I use BodyAsJson for try this functionality. Let me show you my response.

"Response": {
      "StatusCode": 200,
      "BodyAsJson": {
        "Method": "{{request.method}}",
        "MimeType": "{{request.PathSegments.0}}",
        //"response": "{{JsonPath.SelectToken request.bodyAsJson \"$.person\"}}",
        "Data": "{{JsonPath.SelectToken request.body \"$.person[?(@.id == 1)]\"}}"
      },
      "Fault": {
        "Type": "MALFORMED_RESPONSE_CHUNK",
        "Percentage": 0.5
      },
      "UseTransformer": true,
      "Headers": {
        "Content-Type": "application/json; charset=UTF-8"
      }
    }

In my response I would like to use {{request.PathSegments.1}} to get (asterisk) value of my path /json/* Example:
/json/1 => () = 1, /json/2 => () = 2, ... ,/json/n => (*) = n

And I need to use in my filter like this but actually it recognise like a text instead an attribute:

"Response": {
      "StatusCode": 200,
      "BodyAsJson": {
        "Method": "{{request.method}}",
        "MimeType": "{{request.PathSegments.0}}",
        //"response": "{{JsonPath.SelectToken request.bodyAsJson \"$.person\"}}",
        "Data": "{{JsonPath.SelectToken request.body \"$.person[?(@.id == {{request.PathSegments.1}})]\"}}"
      },
      "Fault": {
        "Type": "MALFORMED_RESPONSE_CHUNK",
        "Percentage": 0.5
      },
      "UseTransformer": true,
      "Headers": {
        "Content-Type": "application/json; charset=UTF-8"
      }
    }

Thanks a lot Stef.

@AndresGMD commented on GitHub (Oct 20, 2021): Off course!! I try to implemented a Get By ID using a clean Path For example. In the request of my static file configuration I define the next Wildcardmatcher ``` "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/json/*", "IgnoreCase": false } ] }, "Methods": [ "PUT" ] ``` I use PUT because I don know how to use Filters when I use BodyAsFile Instead (Issue [#658](https://github.com/WireMock-Net/WireMock.Net/issues/658)) I use BodyAsJson for try this functionality. Let me show you my response. ``` "Response": { "StatusCode": 200, "BodyAsJson": { "Method": "{{request.method}}", "MimeType": "{{request.PathSegments.0}}", //"response": "{{JsonPath.SelectToken request.bodyAsJson \"$.person\"}}", "Data": "{{JsonPath.SelectToken request.body \"$.person[?(@.id == 1)]\"}}" }, "Fault": { "Type": "MALFORMED_RESPONSE_CHUNK", "Percentage": 0.5 }, "UseTransformer": true, "Headers": { "Content-Type": "application/json; charset=UTF-8" } } ``` In my response I would like to use {{request.PathSegments.1}} to get (asterisk) value of my path /json/* Example: /json/1 => (*) = 1, /json/2 => (*) = 2, ... ,/json/n => (*) = n And I need to use in my filter like this but actually it recognise like a text instead an attribute: ``` "Response": { "StatusCode": 200, "BodyAsJson": { "Method": "{{request.method}}", "MimeType": "{{request.PathSegments.0}}", //"response": "{{JsonPath.SelectToken request.bodyAsJson \"$.person\"}}", "Data": "{{JsonPath.SelectToken request.body \"$.person[?(@.id == {{request.PathSegments.1}})]\"}}" }, "Fault": { "Type": "MALFORMED_RESPONSE_CHUNK", "Percentage": 0.5 }, "UseTransformer": true, "Headers": { "Content-Type": "application/json; charset=UTF-8" } } ``` Thanks a lot Stef.
Author
Owner

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

Hello @andresendava,

I do understand your question.

However, this specific scenario is not supported (at least not in this form).
Because the HandleBars does not handle / replace two times. So that request.PathSegments.1 not not translated first.

What you need to do is check the handlebars.net / handlebars.js documentation to see if you can write handlebars code which contains two parts:

  1. First replace request.PathSegments.1 and store this in a (temp)-variable?
  2. Then use this variable in the SelectToken call.
@StefH commented on GitHub (Oct 23, 2021): Hello @andresendava, I do understand your question. However, this specific scenario is not supported (at least not in this form). Because the HandleBars does not handle / replace two times. So that `request.PathSegments.1` not not translated first. What you need to do is check the handlebars.net / handlebars.js documentation to see if you can write handlebars code which contains two parts: 1. First replace request.PathSegments.1 and store this in a (temp)-variable? 2. Then use this variable in the SelectToken call.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#375