I have a class, geolocAddress, that I created to deserialize some JSON using JSON2Apex. I really only need to access the variables from one inner class, Components, but when I try to retrieve the variables from that class, they all have a null value.
Here is the code I'm using to retrieve the variables:
query = q.Job_Site_Geolocation__latitude__s + ',' + q.Job_Site_Geolocation__longitude__s;
System.debug('Query text: ' + query);
req.setEndpoint('https://opencage-geocoder.p.rapidapi.com/geocode/v1/json?language=en&key=[API KEY]&q='+ query);
HttpResponse res = h.send(req);
System.debug(res.getBody());
String jsonBody = res.getBody();
geolocAddress g = (geolocAddress)JSON.deserialize(jsonBody, geolocAddress.class);
System.debug('compList: ' + g.cls_results[0].components);
q.Job_Site_City__c =g.cls_results[0].components.locality;
System.debug('q.Job_Site_City__c = ' + q.Job_Site_City__c);
q.Job_Site_Country__c = g.cls_results[0].components.country;
System.debug('q.Job_Site_Country__c = ' + q.Job_Site_City__c);
q.Job_Site_Postal_Zip__c = g.cls_results[0].components.postcode;
System.debug('q.Job_Site_Postal_Zip__c = ' + q.Job_Site_City__c);
q.Job_Site_State_Province__c = g.cls_results[0].components.state;
System.debug('q.Job_Site_State_Province__c = ' + q.Job_Site_City__c);
q.Job_Site_Street__c = g.cls_results[0].components.house_number + ' ' + g.cls_components[0].road;
System.debug('q.Job_Site_Street__c = ' + q.Job_Site_City__c);
q.Job_Site_County__c = g.cls_results[0].components.county;
System.debug('q.Job_Site_County__c = ' + q.Job_Site_County__c);
Here is the classs created by AdminBooster to deserialize the JSON:
public class geolocAddress{
public String documentation; //https://opencagedata.com/api
public cls_licenses[] licenses;
public cls_rate rate;
public cls_results[] results;
public cls_status status;
public cls_stay_informed stay_informed;
public String thanks; //For using an OpenCage API
public cls_timestamp timestamp;
public Integer total_results; //1
class cls_licenses {
public String name; //see attribution guide
public String url; //https://opencagedata.com/credits
}
class cls_rate {
public Integer xlimit; //2500
public Integer remaining; //2499
public Integer reset; //1563408000
}
class cls_results {
public cls_annotations annotations;
public cls_bounds bounds;
public cls_components components;
public Integer confidence; //10
public String formatted; //1456 Franklin Street, Hillside, NJ 07205, United States of America
public cls_geometry geometry;
}
class cls_annotations {
public cls_DMS DMS;
public cls_FIPS FIPS;
public String MGRS; //18TWL6515006095
public String Maidenhead; //FN20vq28ns
public cls_Mercator Mercator;
public cls_OSM OSM;
public cls_UN_M49 UN_M49;
public Integer callingcode; //1
public cls_currency curr;
public String flag; //🇺🇸
public String geohash; //dr5pwcu3218qem03k6md
public Double qibla; //58.33
public cls_roadinfo roadinfo;
public cls_sun sun;
public cls_timezone timezone;
public cls_what3words what3words;
}
class cls_DMS {
public String lat; //40° 42' 11.50632'' N
public String lng; //74° 13' 43.61592'' W
}
class cls_FIPS {
public String county; //34039
public String state; //34
}
class cls_Mercator {
public Double x; //-8263110.235
public Double y; //4940789.883
}
class cls_OSM {
public String edit_url; //https://www.openstreetmap.org/edit?way=11780988#map=17/40.70320/-74.22878
public String url; //https://www.openstreetmap.org/?mlat=40.70320&mlon=-74.22878#map=17/40.70320/-74.22878
}
class cls_UN_M49 {
public cls_regions regions;
public cls_statistical_groupings[] statistical_groupings;
}
class cls_regions {
public String AMERICAS; //019
public String NORTHERN_AMERICA; //021
public String US; //840
public String WORLD; //001
}
class cls_statistical_groupings {
public String x0; //M
public String x1; //E
public String x2; //D
public String x3; //C
}
class cls_currency {
public cls_alternate_symbols[] alternate_symbols;
public String decimal_mark; //.
public String disambiguate_symbol; //US$
public String html_entity; //$
public String iso_code; //USD
public String iso_numeric; //840
public String name; //United States Dollar
public Integer smallest_denomination; //1
public String subunit; //Cent
public Integer subunit_to_unit; //100
public String symbol; //$
public Integer symbol_first; //1
public String thousands_separator; //,
}
class cls_alternate_symbols {
public String x0; //U
public String x1; //S
public String x2; //$
}
class cls_roadinfo {
public String drive_on; //right
public String road; //Franklin Street
public String speed_in; //mph
}
class cls_sun {
public cls_rise rise;
public cls_set xset;
}
class cls_rise {
public Integer apparent; //1563356400
public Integer astronomical; //1563349260
public Integer civil; //1563354480
public Integer nautical; //1563352020
}
class cls_set {
public Integer apparent; //1563323040
public Integer astronomical; //1563330120
public Integer civil; //1563324960
public Integer nautical; //1563327420
}
class cls_timezone {
public String name; //America/New_York
public Integer now_in_dst; //1
public Integer offset_sec; //-14400
public String offset_string; //-0400
public String short_name; //EDT
}
class cls_what3words {
public String words; //hops.badge.awards
}
class cls_bounds {
public cls_northeast northeast;
public cls_southwest southwest;
}
class cls_northeast {
public Double lat; //40.7032962
public Double lng; //-74.2286822
}
class cls_southwest {
public Double lat; //40.7030962
public Double lng; //-74.2288822
}
class cls_components {
public String ISO_3166_1_alpha_2; //US
public String ISO_3166_1_alpha_3; //USA
public String xtype; //building
public String city; //Hillside
public String continent; //North America
public String country; //USA
public String country_code; //us
public String county; //Union County
public String house_number; //1456
public String postcode; //07205
public String road; //Franklin Street
public String state; //New Jersey
public String state_code; //NJ
}
class cls_geometry {
public Double lat; //40.7031962
public Double lng; //-74.2287822
}
class cls_status {
public Integer code; //200
public String message; //OK
}
class cls_stay_informed {
public String blog; //https://blog.opencagedata.com
public String twitter; //https://twitter.com/opencagedata
}
class cls_timestamp {
public String created_http; //Wed, 17 Jul 2019 14:31:08 GMT
public Integer created_unix; //1563373868
}
public static geolocAddress parse(String json){
return (geolocAddress) System.JSON.deserialize(json, geolocAddress.class);
}
}
This is the JSON that is being returned. (I've confirmed that the JSON is being retrieved, so something is happening either in the deserialization or the way I'm calling the inner class variables for Components.)
{
"documentation": "https://opencagedata.com/api",
"licenses": [
{
"name": "see attribution guide",
"url": "https://opencagedata.com/credits"
}
],
"rate": {
"limit": 2500,
"remaining": 2496,
"reset": 1563321600
},
"results": [
{
"annotations": {
"DMS": {
"lat": "40° 42' 11.50632'' N",
"lng": "74° 13' 43.61592'' W"
},
"FIPS": {
"county": "34039",
"state": "34"
},
"MGRS": "18TWL6515006095",
"Maidenhead": "FN20vq28ns",
"Mercator": {
"x": -8263110.235,
"y": 4940789.883
},
"OSM": {
"edit_url": "https://www.openstreetmap.org/edit?way=11780988#map=17/40.70320/-74.22878",
"url": "https://www.openstreetmap.org/?mlat=40.70320&mlon=-74.22878#map=17/40.70320/-74.22878"
},
"UN_M49": {
"regions": {
"AMERICAS": "019",
"NORTHERN_AMERICA": "021",
"US": "840",
"WORLD": "001"
},
"statistical_groupings": [
"MEDC"
]
},
"callingcode": 1,
"currency": {
"alternate_symbols": [
"US$"
],
"decimal_mark": ".",
"disambiguate_symbol": "US$",
"html_entity": "$",
"iso_code": "USD",
"iso_numeric": "840",
"name": "United States Dollar",
"smallest_denomination": 1,
"subunit": "Cent",
"subunit_to_unit": 100,
"symbol": "$",
"symbol_first": 1,
"thousands_separator": ","
},
"flag": "🇺🇸",
"geohash": "dr5pwcu3218qem03k6md",
"qibla": 58.33,
"roadinfo": {
"drive_on": "right",
"road": "Franklin Street",
"speed_in": "mph"
},
"sun": {
"rise": {
"apparent": 1563269940,
"astronomical": 1563262800,
"civil": 1563268020,
"nautical": 1563265560
},
"set": {
"apparent": 1563236700,
"astronomical": 1563243840,
"civil": 1563238620,
"nautical": 1563241080
}
},
"timezone": {
"name": "America/New_York",
"now_in_dst": 1,
"offset_sec": -14400,
"offset_string": "-0400",
"short_name": "EDT"
},
"what3words": {
"words": "hops.badge.awards"
}
},
"bounds": {
"northeast": {
"lat": 40.7032962,
"lng": -74.2286822
},
"southwest": {
"lat": 40.7030962,
"lng": -74.2288822
}
},
"components": {
"ISO_3166-1_alpha-2": "US",
"ISO_3166-1_alpha-3": "USA",
"_type": "building",
"city": "Hillside",
"continent": "North America",
"country": "USA",
"country_code": "us",
"county": "Union County",
"house_number": "1456",
"postcode": "07205",
"road": "Franklin Street",
"state": "New Jersey",
"state_code": "NJ"
},
"confidence": 10,
"formatted": "1456 Franklin Street, Hillside, NJ 07205, United States of America",
"geometry": {
"lat": 40.7031962,
"lng": -74.2287822
}
}
],
"status": {
"code": 200,
"message": "OK"
},
"stay_informed": {
"blog": "https://blog.opencagedata.com",
"twitter": "https://twitter.com/opencagedata"
},
"thanks": "For using an OpenCage API",
"timestamp": {
"created_http": "Tue, 16 Jul 2019 14:48:33 GMT",
"created_unix": 1563288513
},
"total_results": 1
}
What do I need to change in the initial code block in order to retrieve values for the variables in the Components inner class?
replace('"_type"','"x_type"')
on the json body before you deserialize and dispense with jsonParser altogether? – cropredy Jul 16 '19 at 15:30