https://linen.dev logo
#help
Title
# help
r

Roger Williams

08/22/2023, 4:53 PM
Hi, I am having a problem with the math helper returning a String rather than an Integer in the body of a response. Here is the json body:
Copy code
{
  "number": "{{math 1 '*' (jsonPath request.body '$.value')}}"
}
The
value
is coming in as a String and it needs to be an Integer when sent back. The math helper arithmetic operations work correctly but the response is always a String. What am I missing?
o

Oleg Nenashev

08/22/2023, 5:29 PM
Using double quotes enforces the string type in JSON and YAML.. Just try an extension without quotes
l

Lee Turner

08/22/2023, 5:47 PM
Just had a play around with this locally as I have this running standalone. When using the
jsonBody
element to define the response payload, the
"
need to be there for it to be valid json. This means your integer will always go back as a string. If I change the
jsonBody
to use the
body
element instead I don’t have to worry about it being valid json so can remove those quotes. The downside is that you have to escape the json but it seems to work:
Copy code
"response": {
    "status": 202,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{\"number\": {{math 1 '*' 3}} }",
    "transformers": ["response-template"]
  }
Results in:
Copy code
{
  "number": 3
}
r

Roger Williams

08/22/2023, 7:07 PM
That worked! Thanks for the help!
👍 2