I'm using Wiremock standalone and below are sample of request/response. jsonPath is not working and it's returing the whole code. What am I doing wrong?
Sample Request
{
"source": "Web",
"orders":[
{
"id": "Order1",
"status": "In Progress"
},
{
"id": "Order2",
"status": "In Progress"
}
]
}
Expected Response
{
"user": "User1",
"orders": [
{
"id": "Order1",
"status": "In Progress",
"shipping": "Pending"
},
{
"id": "Order2",
"status": "In Progress",
"shipping": "Pending"
}
]
}
My response.json
{
"user": "User1",
"orders": [
{{#each (jsonPath request.body '$.batches') as |batches|}}
{
"id": "{{{batches.id}}}",
"status": "In Progress",
"shipping": "Pending"
}{{^last}},{{/last}}
{{//each}}
]
}
Actual Response
{
"user": "User1",
"orders": [
{{#each (jsonPath request.body '$.batches') as |batches|}}
{
"id": "{{{batches.id}}}",
"status": "In Progress",
"shipping": "Pending"
}{{^last}},{{/last}}
{{//each}}
]
}
Normally, when nothing is parsed it means response-templating isn't enabled for this response. To enable response-templating you can add it to the stub mapping by adding the
transformersfield:With the above stub mapping and this
response.jsonfile:I was able to produce the output you are looking for. This request:
Produces this output from wiremock:
The
jsonPathmatches on the array in the request and theeachloops through those elements. Thenotchecks to see if it is the last element in the loop and if not, add the,to make the response valid json.There is a great explanation of the conditional logic and iteration here - https://docs.wiremock.io/response-templating/conditional-logic-and-iteration/
This was all done using the latest version of wiremock - currently 3.2.0