1

Currently getting raw JSON response -

{  
  "coord": {  
    "lon": 73.85,  
    "lat": 18.52  
  },  
  "weather": [  
    {  
      "id": 800,  
      "main": "Clear",  
      "description": "clear sky",  
      "icon": "01d"  
    }  
  ],  
  "base": "stations",  
  "main": {  
    "temp": 299.077,  
    "pressure": 952.69,  
    "humidity": 41,  
    "temp_min": 299.077,  
    "temp_max": 299.077,  
    "sea_level": 1027.77,  
    "grnd_level": 952.69  
  },  
  "wind": {  
    "speed": 1.86,  
    "deg": 2.50018  
  },  
  "clouds": {  
    "all": 0  
  },  
  "dt": 1544590057,  
  "sys": {  
    "message": 0.0026,  
    "country": "IN",  
    "sunrise": 1544578029,  
    "sunset": 1544617763  
  },  
  "id": 1259229,  
  "name": "Pune",  
  "cod": 200  
}  

Required response after parsing - (example)

Longitude - 73.85

Latitude - 18.52

.......etc


Current Code by which I am getting raw JSON -

public class BotAction {
    
    @InvocableMethod(Label ='Do Callout' description='does a callout')
    public static List<String> DoCallout(List<String> input){
        String city = input.get(0);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.openweathermap.org/data/2.5/weather?q='+city+'&APPID=########################');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            return new List<String> {response.getBody()};
        }
        return new List<String> {'Test'};
    }
}
3

You can use JSON2Apex tool, which can help generate strongly typed Apex code for parsing a JSON structure given an example of the JSON.

JSON2Apex

public class JSON2Apex {

    public class Weather {
        public Integer id;
        public String main;
        public String description;
        public String icon;
    }

    public class Coord {
        public Double lon;
        public Double lat;
    }

    public class Wind {
        public Double speed;
        public Double deg;
    }

    public Coord coord;
    public List<Weather> weather;
    public String base;
    public Main main;
    public Wind wind;
    public Clouds clouds;
    public Integer dt;
    public Sys sys;
    public Integer id;
    public String name;
    public Integer cod;

    public class Clouds {
        public Integer all;
    }

    public class Sys {
        public Double message;
        public String country;
        public Integer sunrise;
        public Integer sunset;
    }

    public class Main {
        public Double temp;
        public Double pressure;
        public Integer humidity;
        public Double temp_min;
        public Double temp_max;
        public Double sea_level;
        public Double grnd_level;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

It depends on the kind of required response but generally, it may look like

public class BotAction {

    @InvocableMethod(Label ='Do Callout' Description='does a callout')
    public static List<String> DoCallout(List<String> input) {
        String city = input.get(0);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.openweathermap.org/data/2.5/weather?q=' + city + '&APPID=########################');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            JSON2Apex parsedResponse = JSON2Apex.parse(response.getBody());
            String lon = String.valueOf(parsedResponse.Coord.lon);
            String lat = String.valueOf(parsedResponse.Coord.lat);
            // etc

            return new List<String>{
                    lon,
                    lat
            };
        }
        else {
            return new List<String>{'Test'};
        }
    }
}
2
  • In the code although you are returning 'lon' & 'lat', but in output only 'lon' is visible/getting printed!
    – user62549
    Dec 12 '18 at 8:45
  • you may specify/put any field you want, the code above was just an example. Dec 12 '18 at 10:30
-1

Hopefully, it's clear for you, that you have to parse it on your own.

There is a good how-to article at the official documentation

If you are still facing some problems or something is unclear, please reply to the post

0

Your Answer

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