Format response field as decimal number instead of string when using JsonPath. #471

Closed
opened 2025-12-29 15:24:37 +01:00 by adam · 7 comments
Owner

Originally created by @robertmircea on GitHub (Dec 2, 2022).

Originally assigned to: @StefH on GitHub.

I am using the following mapping JSON file:

{
  "Guid": "42c42a10-897e-44e1-a50c-f3662193823d",
  "Title": "XYZ",
  "Priority": 100,
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "ExactMatcher",
          "Pattern": "/tpcm/v1/events/oneShot"
        }
      ]
    },
    "Methods": [
      "POST"
    ],
    "Headers": [
      {
        "Name": "Content-Type",
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "application/json",
            "IgnoreCase": true
          }
        ]
      }
    ],
    "Body": {
      "Matcher": {
        "Name": "JsonPartialWildcardMatcher",
        "Pattern": {
          "applicationId": "*",
          "currency": "EUR",
          "msisdn": "*",
          "externalId": "*",
          "transactionDescription": "*"
        },
        "IgnoreCase": false
      }
    }
  },
  "Response": {
    "StatusCode": 200,
    "UseTransformer": true,
    "BodyAsJson": {
      "reservedAmount": "{{JsonPath.SelectToken request.bodyAsJson \"price\"}}",
      "transactionId": "{{Random type='Guid' Format='N'}}"      
    },
    "Headers": {
      "Expires": "0",
      "Content-Type": "application/json; charset=UTF-8",
      "Cache-Control": "no-store, must-revalidate, no-cache, max-age=0",
      "Pragma": "no-cache",
      "Transfer-Encoding": "chunked"
    }
  }
}

Please note the following transformation expression for "reservedAmount" field in the response: "reservedAmount": "{{JsonPath.SelectToken request.bodyAsJson \"price\"}}",

The transformer generates the following json response given that I send the following request to Wiremock standalone server:

{
  "applicationId": "ABA",
  "currency": "EUR",
  "msisdn": "07777",
  "price": 0.005,
  "externalId": "uLGpSXnsCghn",
  "transactionDescription": "ABC"
}

Response:

{
  "reservedAmount": "0,005",
  "transactionId": "6447291E73BD44A78260940456AEDB0E"
}

Two questions:

  1. How can I return the reservedAmount field as JSON decimal number and not as a string?
  2. How can I format the returned number with a decimal dot instead of a comma?
Originally created by @robertmircea on GitHub (Dec 2, 2022). Originally assigned to: @StefH on GitHub. I am using the following mapping JSON file: ``` json { "Guid": "42c42a10-897e-44e1-a50c-f3662193823d", "Title": "XYZ", "Priority": 100, "Request": { "Path": { "Matchers": [ { "Name": "ExactMatcher", "Pattern": "/tpcm/v1/events/oneShot" } ] }, "Methods": [ "POST" ], "Headers": [ { "Name": "Content-Type", "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "application/json", "IgnoreCase": true } ] } ], "Body": { "Matcher": { "Name": "JsonPartialWildcardMatcher", "Pattern": { "applicationId": "*", "currency": "EUR", "msisdn": "*", "externalId": "*", "transactionDescription": "*" }, "IgnoreCase": false } } }, "Response": { "StatusCode": 200, "UseTransformer": true, "BodyAsJson": { "reservedAmount": "{{JsonPath.SelectToken request.bodyAsJson \"price\"}}", "transactionId": "{{Random type='Guid' Format='N'}}" }, "Headers": { "Expires": "0", "Content-Type": "application/json; charset=UTF-8", "Cache-Control": "no-store, must-revalidate, no-cache, max-age=0", "Pragma": "no-cache", "Transfer-Encoding": "chunked" } } } ``` Please note the following transformation expression for "reservedAmount" field in the response: `"reservedAmount": "{{JsonPath.SelectToken request.bodyAsJson \"price\"}}",` The transformer generates the following json response given that I send the following request to Wiremock standalone server: ```json { "applicationId": "ABA", "currency": "EUR", "msisdn": "07777", "price": 0.005, "externalId": "uLGpSXnsCghn", "transactionDescription": "ABC" } ``` Response: ```json { "reservedAmount": "0,005", "transactionId": "6447291E73BD44A78260940456AEDB0E" } ``` Two questions: 1. How can I return the `reservedAmount` field as JSON decimal number and not as a string? 2. How can I format the returned number with a decimal dot instead of a comma?
adam added the question label 2025-12-29 15:24:37 +01:00
adam closed this issue 2025-12-29 15:24:37 +01:00
Author
Owner

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

@robertmircea
You don't need to use JsonPath.SelectToken, you can just access the properties from that request.bodyAsJson.

So:

"BodyAsJson": {
      "reservedAmount": "{{request.bodyAsJson.price}}",
      "transactionId": "{{Random type='Guid' Format='N'}}"      
    },
@StefH commented on GitHub (Dec 3, 2022): @robertmircea You don't need to use JsonPath.SelectToken, you can just access the properties from that `request.bodyAsJson`. So: ``` json "BodyAsJson": { "reservedAmount": "{{request.bodyAsJson.price}}", "transactionId": "{{Random type='Guid' Format='N'}}" }, ```
Author
Owner

@StefH commented on GitHub (Dec 4, 2022):

https://github.com/WireMock-Net/WireMock.Net/pull/858

@StefH commented on GitHub (Dec 4, 2022): https://github.com/WireMock-Net/WireMock.Net/pull/858
Author
Owner

@robertmircea commented on GitHub (Dec 5, 2022):

Your recommendation works but the price field is still formatted incorrectly as a string instead of a number and it uses a comma instead of a decimal dot:

{
"reservedAmount":"0,005",
"transactionId":"B1359682B67647D6A7C96C9CD8D2F37F"
}
@robertmircea commented on GitHub (Dec 5, 2022): Your recommendation works but the price field is still formatted incorrectly as a string instead of a number and it uses a comma instead of a decimal dot: ``` { "reservedAmount":"0,005", "transactionId":"B1359682B67647D6A7C96C9CD8D2F37F" } ```
Author
Owner

@StefH commented on GitHub (Dec 7, 2022):

I'll take a look.

@StefH commented on GitHub (Dec 7, 2022): I'll take a look.
Author
Owner

@StefH commented on GitHub (Dec 7, 2022):

@robertmircea
Can you try preview 1.5.12-ci-16802 ?

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH commented on GitHub (Dec 7, 2022): @robertmircea Can you try preview 1.5.12-ci-16802 ? https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@robertmircea commented on GitHub (Dec 10, 2022):

It works! Looks good! Can you include the fix please in a nuget release?

@robertmircea commented on GitHub (Dec 10, 2022): It works! Looks good! Can you include the fix please in a nuget release?
Author
Owner

@StefH commented on GitHub (Dec 11, 2022):

Yes

I will release a new nuget soon.

@StefH commented on GitHub (Dec 11, 2022): Yes I will release a new nuget soon.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#471