Hi I am working on calculating distance from one location to another. I got the JSON format from the response but further I am not able to get the distance from JSON in my apex code. I have also taken help from this link Distance calculation API but I got the error Error message: Invalid decimal:. I am getting the response in the below format

  "destination_addresses" : [ "New York, NY, USA" ],
  "origin_addresses" : [ "Washington, DC, USA" ],
  "rows" : [
     "elements" : [
           "distance" : {
              "text" : "225 mi",
              "value" : 361993
           "duration" : {
              "text" : "3 hours 52 mins",
              "value" : 13943
           "status" : "OK"
"status" : "OK"

Can anyone tell me please how can I get the value of distance: 225 mi I have also looked JSON2Apex but did not understand how would I use that.

  • What are you doing to get it currently? – pchittum Mar 8 at 22:47
  • I had created an apex class and getting all necessary values there – Nitish Kumar Mar 11 at 7:37

If you put your JSON into the JSON2Apex https://json2apex.herokuapp.com/ and generate the class you can add this class into you Org. Than you can call it like:

JSON2APEX a = JSON2Apex.parse('yourJSONString');

This will give you the distance. If you look at the auto generated class you will see that the class consists of a List of rows. Therefore, you need one of them (here the first one row[0]. A row again ist a list of elements -> elements[0]. An element class have a distance, a duration and a status. Since you need the distance only and than the value of this distance you can use distance.value

| improve this answer | |

I got the solution from the link which I have posted in my question. I generated a class from Json2Apex and perform all the activity in same class. Below is my code -

public class gettingDistance {
       public static String fetchUser(){
           User oUser = [select Id, Name, Location__c FROM User Where id =: userInfo.getUserId()];
           String userName = oUser.Name;
           return userName;

public static Visits__c getgeoLocation(String recordId, String userName){
       Visits__c recordName = [SELECT Id, Name, Distance_km__c, Checked_In_Location__Latitude__s, Checked_In_Location__Longitude__s, Checked_Out_Location__Latitude__s, Checked_Out_Location__Longitude__s FROM Visits__c where Id =: recordId];
       HttpRequest req = new HttpRequest();
       Http http = new Http();
       String url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='+recordName.Checked_In_Location__Latitude__s + ',' + recordName.Checked_In_Location__Longitude__s + '&destinations=' + recordName.Checked_Out_Location__Latitude__s + ',' + recordName.Checked_Out_Location__Longitude__s +'&mode=driving&key=*API KEY*'+'&units=metric'+'&language=en'+'&mode=driving';
       HTTPResponse resp = http.send(req);
       String json = resp.getBody();
       System.debug('Response' +resp.getBody());
       gettingDistance obj = gettingDistance.parse(json);
       gettingDistance.Rows row = (gettingDistance.Rows)obj.rows.get(0);
       gettingDistance.Elements elem = (gettingDistance.Elements)row.elements.get(0);
       gettingDistance.Distance dist = (gettingDistance.Distance)elem.distance;
       String distMile = dist.text;
       Integer distKM = dist.value;
       system.debug('distMile'+ distMile);
       return recordName;
   public class Elements {
       public Distance distance;
       public Distance duration;
       public String status;
   public class Distance {
       public String text;
       public Integer value;
   public List<String> destination_addresses;
   public List<String> origin_addresses;
   public List<Rows> rows;
   public String status;
   public class Rows {
       public List<Elements> elements;
   public static gettingDistance parse(String json) {
       return (gettingDistance) System.JSON.deserialize(json, gettingDistance.class);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.