Forum Discussion

Vivi's avatar
Vivi
Occasional Contributor
5 years ago

The response from JSON tab does not match to the one from RAW when there are zeros in decimal

Hi,

I am using ReadyAPI 2.1.0.

I have the following response in RAW:

{
"amounts": {
"amount": 20000.00,
"amount2": 500.50,
"amount3": 100.55
}
}

 

But the response in JSON looks like this:

{
"amounts": {
"amount": 20000.0,
"amount2": 500.5,
"amount3": 100.55
}
}

It seems that the JSON is deleting the last zeros from the decimal part. I am trying to write a script that will validate that the amounts are up to 8 and must have 2 decimals. This is my script:

import groovy.json.JsonSlurper
def response=messageExchange.response.responseContent
def json=new JsonSlurper().parseText(response)

def amount=json.amounts.amount

assert amount==~ /\d{0,8}\.\d{2}/

But this fails for amount and amount2 as the zero  is not in the json response and when I use log.info for amount I get 20000, for amount2: 500.5 and for amount3: 100.55.

 

Does someone know how to fix this?

 

 

7 Replies

  • avidCoder's avatar
    avidCoder
    Super Contributor

    Please change the code to this line :-

    import groovy.json.JsonSlurper
    def response=messageExchange.response.responseContent
    def json=new JsonSlurper().parseText(response)
    
    float amount=json.amounts.amount
    
    assert amount==~ /\d{0,8}\.\d{2}/
    • Vivi's avatar
      Vivi
      Occasional Contributor

      Hi, 

       

      The float is not solving the problem. For example the "amount": 20000.00 (RAW) is now shown as 20000.0 in json and when I have the log.info for this node. 

       

      So the assertion is still failing even if I declare it as float or double.