1

I have scenario, we need to read the latitude and Longitude from json response body. The latitude and longitude has value like below

"data": [{
  "city": "Mumbai",
  "country": "India",  
  "latitude": 19.0886993408,
  "longitude": 72.8678970337
  
}

when We try to validate the JSON response by reading the latitude and longitude using JSONPATH using getString() then it round off the value after 4 digit in decimal places.

Below is the code in java

String key = item.getKey();
String value = item.getValue();
JsonPath jsonPathEvaluator = response.jsonPath();
String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", "");

Output display:

Latitude:-19.0887 instead of 19.0886993408

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Vipin Singh
  • 109
  • 1
  • 14
  • could you please try `String.format("%.4f %%", value);` – Sabesan Aug 17 '20 at 07:42
  • value is already coming from excel, here using the "key" we are reading the json response body. key=data.latitude, which is passed into getString but getString only return 19.0887 – Vipin Singh Aug 17 '20 at 07:50

1 Answers1

1

You can try with JsonPath.config as mentioned here

Using it you can provide your custom configuration for JsonPath. Here i am just trying to convert my numeric data with long decimal range double.

String key = item.getKey();
String value = item.getValue();
JsonPath jsonPathEvaluator = response.jsonPath();
JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);
String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", "");


/* String json = "{\n" +
"  \"data\": {\n" +
"    \"city\": \"Mumbai\",\n" +
"    \"country\": \"India\",\n" +
"    \"latitude\": 19.0886993408,\n" +
"    \"longitude\": 72.8678970337\n" +
"  }\n" +
"}";
JsonPath jsonPath = JsonPath.from(json);
JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);
double strjson = jsonPath.get("data.latitude");
    
System.out.println(String.valueOf(strjson)); */

For me it's printing below output.

19.0886993408

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
  • Hi, we are reading the input from excel where data.latitude=19.0886993408 store as expected. So we read key =data.latitude. key=data.city which we pass into code line as "String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", ""); ", in this data.city is coming properly but data.latutude is round off . we cannot use the above code only for latitude, we are developing framework. Please suggest some common functions – Vipin Singh Aug 17 '20 at 09:23
  • Just try to add this line before `JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);` before last line. – Sagar Gangwal Aug 17 '20 at 09:41
  • @VipinSingh , You can try updated answer. It will work. – Sagar Gangwal Aug 17 '20 at 09:45
  • here instead of double strjson = jsonPath.get("data.latitude"); . I am using this as double strjson = jsonPath.get(key); its giving me error – Vipin Singh Aug 17 '20 at 10:00
  • can you modify the above code , String json, from where r u picking this. – Vipin Singh Aug 17 '20 at 10:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219953/discussion-between-sagar-gangwal-and-vipin-singh). – Sagar Gangwal Aug 17 '20 at 10:01
  • 1
    String key = item.getKey(); String value = item.getValue(); JsonPath jsonPathEvaluator = response.jsonPath(); JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE); String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", ""); – Sagar Gangwal Aug 17 '20 at 10:05
  • I need help , Can u please help me> – Vipin Singh Oct 12 '20 at 09:06
  • @VipinSingh Sure Vipin. – Sagar Gangwal Oct 15 '20 at 13:43