4

I am trying to parse a JSON file which has a space between two strings.

{
    "result": {
        "input": {
            "address": {
                "street": "1500 PENNSYLVANIA AVE NW",
                "zip": "20502",
                "city": "WASHINGTON",
                "state": "DC"
            },
            "benchmark": {
                "benchmarkName": "Public_AR_Current",
                "isDefault": false,
                "benchmarkDescription": "Public Address Ranges - Current Benchmark",
                "id": "4"
            },
            "vintage": {
                "isDefault": true,
                "vintageName": "Current_Current",
                "vintageDescription": "Current Vintage - Current Benchmark",
                "id": "4"
            }
        },
        "addressMatches": [{
            "geographies": {
                "114th Congressional Districts": [{
                    "OID": 211904692355590,
                    "MTFCC": "G5200",
                    "FUNCSTAT": "N",
                    "STATE": "11",
                    "AREAWATER": 18633403,
                    "NAME": "Delegate District (at Large)",
                    "CDSESSN": "114",
                    "LSADC": "C4",
                    "CENTLON": "-077.0162745",
                    "GEOID": "1198",
                    "CENTLAT": "+38.9047579",
                    "CD114": "98",
                    "INTPTLON": "-077.0172290",
                    "AREALAND": 158364990,
                    "BASENAME": "Delegate District (at Large)",
                    "INTPTLAT": "+38.9041031",
                    "OBJECTID": 254
                }]
            },
            "matchedAddress": "1500 PENNSYLVANIA AVE NW, WASHINGTON, DC, 20220",
            "coordinates": {
                "x": -77.03376,
                "y": 38.89877
            },
            "addressComponents": {
                "preQualifier": "",
                "preDirection": "",
                "preType": "",
                "streetName": "PENNSYLVANIA",
                "suffixType": "AVE",
                "suffixDirection": "NW",
                "suffixQualifier": "",
                "zip": "20220",
                "city": "WASHINGTON",
                "fromAddress": "1500",
                "toAddress": "1598",
                "state": "DC"
            },
            "tigerLine": {
                "tigerLineId": "76225866",
                "side": "L"
            }
        }]
    }
}

I used JSON2Apex tool to covert this Json to Apex class.

Here is the class file:

public class JSON2Apex {

    public class Input {
        public Address address;
        public Benchmark benchmark;
        public Vintage vintage;
    }

    public class Address {
        public String street;
        public String zip;
        public String city;
        public String state;
    }

    public Result result;

    public class CongressionalDistricts {
        public Long OID;
        public String MTFCC;
        public String FUNCSTAT;
        public String STATE;
        public Integer AREAWATER;
        public String NAME;
        public String CDSESSN;
        public String LSADC;
        public String CENTLON;
        public String GEOID;
        public String CENTLAT;
        public String CD114;
        public String INTPTLON;
        public Long AREALAND;
        public String BASENAME;
        public String INTPTLAT;
        public Integer OBJECTID;
    }

    public class TigerLine {
        public String tigerLineId;
        public String side;
    }

    public class Coordinates {
        public Double x;
        public Double y;
    }

    public class Benchmark {
        public String benchmarkName;
        public Boolean isDefault;
        public String benchmarkDescription;
        public String id;
    }

    public class AddressComponents {
        public String preQualifier;
        public String preDirection;
        public String preType;
        public String streetName;
        public String suffixType;
        public String suffixDirection;
        public String suffixQualifier;
        public String zip;
        public String city;
        public String fromAddress;
        public String toAddress;
        public String state;
    }

    public class Geographies {
        public List<CongressionalDistricts> CongressionalDistricts;
    }

    public class Vintage {
        public Boolean isDefault;
        public String vintageName;
        public String vintageDescription;
        public String id;
    }

    public class AddressMatches {
        public Geographies geographies;
        public String matchedAddress;
        public Coordinates coordinates;
        public AddressComponents addressComponents;
        public TigerLine tigerLine;
    }

    public class Result {
        public Input input;
        public List<AddressMatches> addressMatches;
    }


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

The issue which I am facing is when i try to deserialize this code I am getting Null value in CongressionalDistrict. In my code I am replacing the Json string "114th Congressional District" with CongressionalDistrict . After replacing also I am getting Null value.

Here is the code which I am using.

Http http = new Http();
    HttpRequest request = new HttpRequest();
    HttpResponse response = new HttpResponse();

    String urlAdd = 'https://geocoding.geo.census.gov/geocoder/geographies/address?street=1500+PENNSYLVANIA+AVE+NW&city=WASHINGTON&state=DC&zip=20502&benchmark=4&vintage=4&layers=54&format=json';
    request.setEndpoint(urlAdd);
    request.setMethod('GET');
    response = http.send(request);
    String jString = response.getBody();
    System.debug('** jString *** '+jString);
    jString = jString.replace('114th Congressional Districts', 'CongressionalDistricts:');

    if (response.getStatusCode() == 200) {
        JSON2Apex resultJSON2Apex = (JSON2Apex) JSON.deserialize(response.getBody(), JSON2Apex.class);          
        System.debug('#### resultJSON2Apex ###### '+resultJSON2Apex);
    }
2

That is odd - I have used that technique in the past and it worked. I ran the same code and got the same results, so it is something odd with the parsing I think. As a workaround, yon can use the deserializeUntyped method, and that seems to work for me in so far as I can then see the Congressional District data in the Map:

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(jString);

Actually, I see the issue once I used strict to deserialize - you didn't replace res.getbody() with jstring in the parsing:

if (response.getStatusCode() == 200) {
        JSON2Apex resultJSON2Apex = (JSON2Apex) JSON.deserialize(**response.getBody()**, JSON2Apex.class);          
        System.debug('#### resultJSON2Apex ###### '+resultJSON2Apex);
    }
  • Ha. Find and replace...and discard. Nice catch! – Adrian Larson Aug 31 '16 at 1:13
2

There's an error in your code: You are replacing '114th Congressional Districts' with 'CongressionalDistricts:'

You only included the colon in the second String literal, so you will end up with 'CongressionalDistricts::'

Change it to:

 jString = jString.replace('114th Congressional Districts', 'CongressionalDistricts');
  • My mistake i thought I removed ':' from my code. No still I am getting Null value. – Tamil Aug 30 '16 at 21:31

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.