Hello dear community! Need your help! Given stub t...
# wiremock-java
p
Hello dear community! Need your help! Given stub to map value from Json request body to Response:
{{{jsonPath request.body '$..paths[1].source'}}}
Copy code
{
  "request": {
    "urlPattern": ".*/transfers",
    "method": "POST"
  },
  "response": {
    "status": 200,
    "body": "{\"id\":  \"{{{jsonPath request.body '$..paths[1].source'}}}\",
Request is a valid Json structure containing this part in its body, and I need to get blablablah2 from request body to return it in response body:
Copy code
{
  "paths": [
    {
      "source": "blablablah1",
      "destination": "blablablah1"
    },
    {
      "source": "blablablah2",
      "destination": "blablablah2"
    }
  ]
}
My issue is that Wiremock returns:
"body": "{\"id\":  \"[\"*blablablah2*\"]\",
and I'd like to get it without
[ ]
, any idea why it's happening?
Actual:
"body": "{\"id\":  \"[\"*blablablah2*\"]\",
Expected:
"body": "{\"id\":  \"*blablablah2*\",
👀 1
t
Hi @Pavel Serada Have you tried something like:
Copy code
{{{jsonPath request.body '$..paths[1].source[0]'}}}
a
@Pavel Serada I think it is your double
.
between the
$
and the rest of the expression. The following returned what you’d expect:
Copy code
"response": {
    "status": 200,
    "body": "{\"id\":  \"{{{jsonPath request.body '$.paths[1].source'}}}\" }"
  }
Copy code
{
  "id": "blablablah2"
}
As for why… I don’t know 🤷🏻
t
$..
is a query, meaning it always returns a list of results
Whereas a single
.
indicates a selector, which is potentially only one element.
a
That’s what OP wants in this case, right? Just one element, instead of a list/array?
t
Yes, I think your solution is probably the more elegant one!
👍🏻 1
a
😅 One day I’ll learn jsonPath querying syntax… But today is not that day 😛
p
going to give it a try in a sec, thanks guys!
It worked! really appreciate your help, guys! @Tom @Aaron!!!
👍🏻 1
👍 1