0

I have two JSON objects represented as Java Strings Expected:

{
"id" : "1234567"
"balance" : "123"
}

I converted this into JSON object and trying to validate this with output JSON Actual:

{
"id" : "1234567"
"balance" : "123.00"
}

I am using JSONAssert.assertequal("incorrect json",actual,expected,false);

This is failing with expected 123 and got 123.00.

what to do so that it will treat both the values same.in a nutshell how do I use custom comaparator ,please note that I dont want to exclude/suppress this attribute from my validation

1 Answers1

0

The balance field in the Java class should be defined as an int (or float), not a String.

If applicable, it should be defined as a “number” in any JsonSchema, swagger or open-api definition.

The ‘expected’ can be defined as either of the following and the JSONAssert.assertEquals will work without a custom comparator - see Introduction to JSONassert section 3.4 Logical Camparison:

{
"id" : "1234567"
"balance" : 123
}

or

{  
"id" : "1234567"
"balance" : 123.00
}

If you do not have control of the field types then a comparator for the balance field would look like this:

public int compare(String firstBalance, String secondBalance) {
       return
Float.compare(Float.parseFloat(firstBalance), Float.parseFloat(secondBalance));
    }
John Williams
  • 4,252
  • 2
  • 9
  • 18