function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Milan HrdlickaMilan Hrdlicka 

get json data in a variable using URL

Hello,
I have this URL which generates JSON output:
https://maps.googleapis.com/maps/api/geocode/json?address= Nuselská 23, 140 00, Praha 4 key=AIzaSyC2Hu3fh2L7MIN_UaBqgBtM-QvI-SPcGYg
I get data in JSON format using above URL and what I need is to get geolocation data (latitude and longtitude) and place these in a variabĺe.
Could anyoune please share a code how to do that.
I just need to place the URL in a code and get required geolocoation details without any "in between" step of copying/pasting JSON output format.
Appreciate your help,
Milan
Jim JamJim Jam
Firstly, create an Apex class which defines the JSON response ..

public class googleAddress {

    public class Address_components {
        public String long_name;
        public String short_name;
        public List<String> types;
    }

    public class Geometry {
        public Location location;
        public String location_type;
        public Viewport viewport;
    }

    public List<Results> results;
    public String status;
    public Location firstLoc;

    public class Results {
        public List<Address_components> address_components;
        public String formatted_address;
        public Geometry geometry;
        public Boolean partial_match;
        public String place_id;
        public List<String> types;
    }

    public class Viewport {
        public Location northeast;
        public Location southwest;
    }

    public class Location {
        public Double lat;
        public Double lng;
    }

    
    public static googleAddress parse(String json) {
        
        googleAddress returnAddr;
        
        returnAddr = (googleAddress) System.JSON.deserialize(json, googleAddress.class);
        if (!returnAddr.results.isEmpty()){
            returnAddr.firstLoc = returnAddr.results[0].geometry.location;
        }
        return returnAddr; 
        
        
    }
}


Then, assuming only one address is returned in the response, when you make the api call and retrieve the JSON string, you can parse it and get the location details with something like ..

        googleAddress addr = googleAddress.parse(jsonString);
        googleAddress.Location loc = addr.firstLoc;
        double lng = loc.lng;
        double lat = loc.lat;

 
Milan HrdlickaMilan Hrdlicka
Hi Jim, many thanks for your response, however I still have some questions as I am completely new to this...

I have created below class to make the web callout:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class JSONpokus1 {
    
    @future(callout=true)
      public static void parseJSONResponse() {
          Http httpProtocol = new Http();
          HttpRequest request = new HttpRequest();
          String endpoint = 'http://maps.googleapis.com/maps/api/geocode/json?address=Nuselská 23, 140 00, Praha 4';
          request.setEndPoint(endpoint);
          request.setMethod('GET');
          HttpResponse response = httpProtocol.send(request);
          String jsonString = response.getBody();
          System.debug(jsonString);
          
          
      }
                        
}
​-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

and as its static class I called this in developer console like this:

JSONpokus1.parseJSONResponse();

​-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
So the jsoon output is now placed in variable "jsonString" of type String and I succesfully confirmed the output is the same as when I place the link in a web browser and press enter.
My question is how to combine my code with yours above to get all of this in one piece and also how to use the retrieved "lng" and "lat" data, new to OOP "making experiments" at this stage, sorry for bothersome questions :)
Appreciate your help, Milan.
Jim JamJim Jam
Assuming you've created the googleAddress class shown earlier, you just need to add the following lines to your JSONpokus1 class, after  
...
String jsonString = response.getBody();
System.debug(jsonString);

googleAddress addr = googleAddress.parse(jsonString);
googleAddress.Location loc = addr.firstLoc;
double lng = loc.lng;
double lat = loc.lat;
Milan HrdlickaMilan Hrdlicka
Amazing, it works, really appreciate your help on this! Milan
hemankshree takhemankshree tak
@Jim Jam works absolutely fine