Forum Discussion

indraniria's avatar
indraniria
Contributor
12 months ago

How I cast a numeric value in json payload?

I have a requirement to generate a random decimal number each time my automated test runs. I have below payload where I would like to put a numeric value(in the highlighted field).

How to I do this?

"GroupHeader":{
"messageIdentification": "${#TestCase#MSGID}",
"messageCreationDateTime": "2023-03-20T04:22:59.093",
"sumAmountFromTransactions": 20.26,
"totalNumberOfTransactions": "1"
},

 

I tried to generate a number and store in properties. But it throws error.

 

4 Replies

  • Hello,

    line number 6 in your picture should end in .toString() also like line number 4 did.  Properties only accept string values.

     

    ${#TestCase#WireAmount}   can then be used in the header like you did for MSGID

  • But I need to use amount as Integer, hence can not pull from property.
    How else I can create a numeric value at run time? Amount field accepts only integer value in the payload.
    • TNeuschwanger's avatar
      TNeuschwanger
      Champion Level 2

      Hello,

       

      I don't know the context of how you are using your group header, but it looks a little json'ish...

      From your example ("sumAmountFromTransactions": 20.26,), there are no double quotes around the number hence it is numeric value.  If you enclosed your 20.26 in double quotes, that would make it an alpha-numeric value... a sting value.

      Here is example of nice numeric value at run time...

      "GroupHeader":{
      "messageIdentification": "${#TestCase#MSGID}",
      "messageCreationDateTime": "2023-03-20T04:22:59.093",
      "sumAmountFromTransactions": ${#TestCase#WireAmount},
      "totalNumberOfTransactions": "1"
      },

       

      Here is example of bad numeric value at run time that would be not numeric but alphanumeric (notice this sample has double quotes which do not let it conform to what you need)...

      "GroupHeader":{
      "messageIdentification": "${#TestCase#MSGID}",
      "messageCreationDateTime": "2023-03-20T04:22:59.093",
      "sumAmountFromTransactions": "${#TestCase#WireAmount}",
      "totalNumberOfTransactions": "1"
      },

      Even though it is stored in properties as string like all properties are, you can use it as a number when needed depending on how you use it (or enclose it in this case).

       

      It looks like your totalNumberOfTransactions is defined as string value also even though it seems like a total of something should be numeric.  Since it is enclosed by double quotes, this example seems suspect that this sumAmountFromTransactions might not be your only problem unless everything is double quoted and my nice numeric example is wrong to begin with.  🙂

       

      Also you say you need the value as an integer in your followup question...  your original question stated needed decimal digits as shown in your original example of 20.26.  It is hard to answer when the question changes between replys.  🙂