2

I am facing the issue like "Illegal Value for primitive" when trying to parse the json data.Below is the Json Response which i get from a service.

[{"revenue": {}, "emails": [{"grade": "A+", "type": "info", "email": "massimo.cavalleri@kuldat.com"}, {"grade": "B", "type": "info", "email": "jacopo.serafin@kuldat.com"}, {"grade": "B", "type": "info", "email": "simone.marzola@kuldat.com"}], "lead_enrichment_date": "2016-01-27", "name": {"raw": "Kuldat sr.l", "formatted": "Kuldat s.r.l.", "business_type": "", "business_alias": "Kuldat", "business_name": ""}, "year_of_establishment": 0, "industry": {"codes": [], "raw": ["INFOANDTECHSERV"]}, "size": {}, "ranking": {"delta": -332533, "phone": 3, "social": 5, "value": 5, "email": 92}, "automation": [], "enrichment_date": "2016-01-27", "country": "IT", "leads": [{"seniority": {"month": 0, "year": 0, "raw": ""}, "name": "Massimo Cavalleri", "current": true, "status": "checked", "role": "developer", "links": [{"contacts": 0, "validated": true, "source": "linkedin", "url": "https://it.linkedin.com/in/massimo-cavalleri-14a7a121"}]}, {"seniority": {"month": 0, "year": 0, "raw": ""}, "name": "Simone Marzola", "current": true, "status": "checked", "role": "CTO", "links": [{"contacts": 0, "validated": true, "source": "linkedin", "url": "https://it.linkedin.com/in/marzolasimone"}]}], "feedback": null, "tax_id": {"formatted": "IT03513190987", "raw": "03513190987"}, "websites": ["http://www.kuldat.com"], "description": [{"validated": true, "language": "en", "source": "", "text": "kuldat test"}, {"validated": true, "language": "it", "source": "", "text": "kuldat test"}], "phones": [{"raw": "02.35925100", "area_code": "02", "country": "IT", "country_code": 39, "number": "35925100", "description": "Milano", "formatted": "+390235925100"}], "address": {"location": [12.4975443, 41.888395], "formatted": "Via Trieste, 25, 25121, Brescia, BS, Italy", "details": {"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", "permanently_closed": "", "url": "https://www.google.it/maps/place/Via+Trieste,+25,+25121+Brescia+BS/@45.5373451,10.223621,17z/data=!3m1!4b1!4m2!3m1!1s0x47817614394a84a7:0x540aadf97192856a", "name": "Via Trieste, 25", "place_id": "ChIJT5F8Q7hhLxMRgBE2qMxsvec", "website": "", "utc_offset": "", "rating": "", "international_phone_number": "", "vicinity": "Roma", "types": ["street_address"]}, "source": "gplaces", "raw": "25, Via Trieste, 25121, Brescia, BS, Italy"}, "links": [{"contacts": 0, "validated": true, "source": "paginegialle", "url": "http://www.paginegialle.it/kuldat"}, {"contacts": 0, "validated": false, "source": "website", "url": "http://www.kuldat.com"}], "id": "569f6d6fe4b0ccecd3ef1390"}]

And my parser class is like below

public class JsonParser{
    public String feedback{get;set;}
    public tax_id tax_id{get;set;}
    public list<String> websites{get;set;}
    public list<leads> leads{get;set;}
    public list<description> description{get;set;}
    public String country{get;set;}
    public list<phones> phones{get;set;}
    public String enrichment_date{get;set;}
    public size size{get;set;}
    public industry industry{get;set;}
    public ranking ranking{get;set;}
    public Integer year_of_establishment{get;set;}
    public list<String> automation{get;set;}
    public name name{get;set;}
    public address address{get;set;}
    public String lead_enrichment_date{get;set;}
    public list<links> links{get;set;}
    public list<emails> emails{get;set;}
    public String id{get;set;}
    public revenue revenue{get;set;}
public class tax_id{
    public String raw{get;set;}
    public String formatted{get;set;}
}
public class revenue{
} 
public class seniority{
    public Integer year{get;set;}
    public String raw{get;set;}
    public Integer month{get;set;}
}
public class ranking{
    public Integer email{get;set;}
    public Integer value{get;set;}
    public Integer social{get;set;}
    public Integer phone{get;set;}
    public Integer delta{get;set;}
}
public class phones{
    public String raw{get;set;}
    public String area_code{get;set;}
    public String country{get;set;}
    public String description{get;set;}
    public String r_number{get;set;}
    public String formatted{get;set;}
    public Integer country_code{get;set;}
}
public class name{
    public String business_name{get;set;}
    public String business_type{get;set;}
    public String formatted{get;set;}
    public String business_alias{get;set;}
    public String raw{get;set;}
}
public class links{
    public Integer contacts{get;set;}
    public String source{get;set;}
    public String url{get;set;}
    public Boolean validated{get;set;}      
}
public class leads{
    public list<links> links{get;set;}
    public Boolean current{get;set;}
    public String status{get;set;}
    public String name{get;set;}
    public String role{get;set;}
    public seniority seniority{get;set;}
}
public class industry{
    public list<String> codes{get;set;}
    public list<String> raw{get;set;}
}
public class emails{
    public String email{get;set;}
    public String type{get;set;}
    public String grade{get;set;}
}
public class details{
    public String icon{get;set;}
    public String utc_offset{get;set;}
    public String website{get;set;}
    public String rating{get;set;}
    public String place_id{get;set;}
    public String international_phone_number{get;set;}
    public String name{get;set;}
    public String vicinity{get;set;}
    public String url{get;set;}
    public list<String> types{get;set;}
    public String permanently_closed{get;set;}
}
public class description{
    public String text{get;set;}
    public String language{get;set;}
    public String source{get;set;}
    public Boolean validated{get;set;}
}
public class address{
    public String formatted{get;set;}
    public String source{get;set;}
    public details details{get;set;}
    public String raw{get;set;}
    public list<String> location{get;set;}
}
}

Can anyone figure me what exactly is i am missing. And why do we get that error. Thanks in Advance.

  • Created Parser class using the same. – sunny Jan 30 '16 at 11:27
  • 1
    this class is invalid from various point of view. 1. public class links contains duplicate variables. 2. phones class contains number which is reserved keyword. try to create 3. revenue is defined as property but it doesn't contain any class with that name. first fix these issue and try to save your apex class. – Himanshu Jan 30 '16 at 11:30
  • @Himanshu : I have updated the code as per your suggestions. But still the same issue. – sunny Jan 30 '16 at 11:54
  • This kind of problem is subject to diagnosis via selective reduction. Make the incoming JSON simpler and deserialize. If the simpler JSON deserializes, add in more of it, then rinse and repeat – cropredy Jan 30 '16 at 17:18
3

To parse a JSON into a class, you need to check below points first:

If you are using standard JSON.deserialize method you need to check that json should not have any key which is reserved key word in salesforce

Json must not have any key which is not of type string

If you are going to parse your json manually, you can have any valid json and parse it in your class varraible as per your need.

So if you want an easy solution and have rights to change the JSON, Please change the JSON so that you can simply parse it using JSON.Serialize method.

If you cant change the JSON you need to parse it manually like below

public class JSON2Apex {
    public static void consumeObject(JSONParser parser) {
        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            if (curr == JSONToken.START_OBJECT || 
                curr == JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == JSONToken.END_OBJECT ||
                curr == JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }

    public class Description {
        public Boolean validated {get;set;} 
        public String language {get;set;} 
        public String source {get;set;} 
        public String text {get;set;} 

        public Description(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'validated') {
                            validated = parser.getBooleanValue();
                        } else if (text == 'language') {
                            language = parser.getText();
                        } else if (text == 'source') {
                            source = parser.getText();
                        } else if (text == 'text') {
                            text = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Description consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Address {
        public List<Double> location {get;set;} 
        public String formatted {get;set;} 
        public Details details {get;set;} 
        public String source {get;set;} 
        public String raw {get;set;} 

        public Address(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'location') {
                            location = new List<Double>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                                location.add(new Double(parser));
                            }
                        } else if (text == 'formatted') {
                            formatted = parser.getText();
                        } else if (text == 'details') {
                            details = new Details(parser);
                        } else if (text == 'source') {
                            source = parser.getText();
                        } else if (text == 'raw') {
                            raw = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Address consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public Revenue revenue {get;set;} 
    public List<Emails> emails {get;set;} 
    public String lead_enrichment_date {get;set;} 
    public Name name {get;set;} 
    public Integer year_of_establishment {get;set;} 
    public Industry industry {get;set;} 
    public Revenue size {get;set;} 
    public Ranking ranking {get;set;} 
    public List<Revenue> automation {get;set;} 
    public String enrichment_date {get;set;} 
    public String country {get;set;} 
    public List<Leads> leads {get;set;} 
    public Object feedback {get;set;} 
    public Tax_id tax_id {get;set;} 
    public List<String> websites {get;set;} 
    public List<Description> description {get;set;} 
    public List<Phones> phones {get;set;} 
    public Address address {get;set;} 
    public List<Links> links {get;set;} 
    public String id {get;set;} 

    public JSON2Apex(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'revenue') {
                        revenue = new Revenue(parser);
                    } else if (text == 'emails') {
                        emails = new List<Emails>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            emails.add(new Emails(parser));
                        }
                    } else if (text == 'lead_enrichment_date') {
                        lead_enrichment_date = parser.getText();
                    } else if (text == 'name') {
                        name = new Name(parser);
                    } else if (text == 'year_of_establishment') {
                        year_of_establishment = parser.getIntegerValue();
                    } else if (text == 'industry') {
                        industry = new Industry(parser);
                    } else if (text == 'size') {
                        size = new Revenue(parser);
                    } else if (text == 'ranking') {
                        ranking = new Ranking(parser);
                    } else if (text == 'automation') {
                        automation = new List<Revenue>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            automation.add(new Revenue(parser));
                        }
                    } else if (text == 'enrichment_date') {
                        enrichment_date = parser.getText();
                    } else if (text == 'country') {
                        country = parser.getText();
                    } else if (text == 'leads') {
                        leads = new List<Leads>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            leads.add(new Leads(parser));
                        }
                    } else if (text == 'feedback') {
                        feedback = new Object(parser);
                    } else if (text == 'tax_id') {
                        tax_id = new Tax_id(parser);
                    } else if (text == 'websites') {
                        websites = new List<String>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            websites.add(new String(parser));
                        }
                    } else if (text == 'description') {
                        description = new List<Description>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            description.add(new Description(parser));
                        }
                    } else if (text == 'phones') {
                        phones = new List<Phones>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            phones.add(new Phones(parser));
                        }
                    } else if (text == 'address') {
                        address = new Address(parser);
                    } else if (text == 'links') {
                        links = new List<Links>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            links.add(new Links(parser));
                        }
                    } else if (text == 'id') {
                        id = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }

    public class Leads {
        public Seniority seniority {get;set;} 
        public String name {get;set;} 
        public Boolean current {get;set;} 
        public String status {get;set;} 
        public String role {get;set;} 
        public List<Links> links {get;set;} 

        public Leads(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'seniority') {
                            seniority = new Seniority(parser);
                        } else if (text == 'name') {
                            name = parser.getText();
                        } else if (text == 'current') {
                            current = parser.getBooleanValue();
                        } else if (text == 'status') {
                            status = parser.getText();
                        } else if (text == 'role') {
                            role = parser.getText();
                        } else if (text == 'links') {
                            links = new List<Links>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                                links.add(new Links(parser));
                            }
                        } else {
                            System.debug(LoggingLevel.WARN, 'Leads consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Tax_id {
        public String formatted {get;set;} 
        public String raw {get;set;} 

        public Tax_id(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'formatted') {
                            formatted = parser.getText();
                        } else if (text == 'raw') {
                            raw = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Tax_id consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Seniority {
        public Integer month {get;set;} 
        public Integer year {get;set;} 
        public String raw {get;set;} 

        public Seniority(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'month') {
                            month = parser.getIntegerValue();
                        } else if (text == 'year') {
                            year = parser.getIntegerValue();
                        } else if (text == 'raw') {
                            raw = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Seniority consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Name {
        public String raw {get;set;} 
        public String formatted {get;set;} 
        public String business_type {get;set;} 
        public String business_alias {get;set;} 
        public String business_name {get;set;} 

        public Name(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'raw') {
                            raw = parser.getText();
                        } else if (text == 'formatted') {
                            formatted = parser.getText();
                        } else if (text == 'business_type') {
                            business_type = parser.getText();
                        } else if (text == 'business_alias') {
                            business_alias = parser.getText();
                        } else if (text == 'business_name') {
                            business_name = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Name consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Industry {
        public List<Revenue> codes {get;set;} 
        public List<String> raw {get;set;} 

        public Industry(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'codes') {
                            codes = new List<Revenue>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                                codes.add(new Revenue(parser));
                            }
                        } else if (text == 'raw') {
                            raw = new List<String>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                                raw.add(new String(parser));
                            }
                        } else {
                            System.debug(LoggingLevel.WARN, 'Industry consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Ranking {
        public Integer delta {get;set;} 
        public Integer phone {get;set;} 
        public Integer social {get;set;} 
        public Integer value {get;set;} 
        public Integer email {get;set;} 

        public Ranking(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'delta') {
                            delta = parser.getIntegerValue();
                        } else if (text == 'phone') {
                            phone = parser.getIntegerValue();
                        } else if (text == 'social') {
                            social = parser.getIntegerValue();
                        } else if (text == 'value') {
                            value = parser.getIntegerValue();
                        } else if (text == 'email') {
                            email = parser.getIntegerValue();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Ranking consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Details {
        public String icon {get;set;} 
        public String permanently_closed {get;set;} 
        public String url {get;set;} 
        public String name {get;set;} 
        public String place_id {get;set;} 
        public String website {get;set;} 
        public String utc_offset {get;set;} 
        public String rating {get;set;} 
        public String international_phone_number {get;set;} 
        public String vicinity {get;set;} 
        public List<String> types {get;set;} 

        public Details(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'icon') {
                            icon = parser.getText();
                        } else if (text == 'permanently_closed') {
                            permanently_closed = parser.getText();
                        } else if (text == 'url') {
                            url = parser.getText();
                        } else if (text == 'name') {
                            name = parser.getText();
                        } else if (text == 'place_id') {
                            place_id = parser.getText();
                        } else if (text == 'website') {
                            website = parser.getText();
                        } else if (text == 'utc_offset') {
                            utc_offset = parser.getText();
                        } else if (text == 'rating') {
                            rating = parser.getText();
                        } else if (text == 'international_phone_number') {
                            international_phone_number = parser.getText();
                        } else if (text == 'vicinity') {
                            vicinity = parser.getText();
                        } else if (text == 'types') {
                            types = new List<String>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                                types.add(new String(parser));
                            }
                        } else {
                            System.debug(LoggingLevel.WARN, 'Details consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Phones {
        public String raw {get;set;} 
        public String area_code {get;set;} 
        public String country {get;set;} 
        public Integer country_code {get;set;} 
        public String number_Z {get;set;} // in json: number
        public String description {get;set;} 
        public String formatted {get;set;} 

        public Phones(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'raw') {
                            raw = parser.getText();
                        } else if (text == 'area_code') {
                            area_code = parser.getText();
                        } else if (text == 'country') {
                            country = parser.getText();
                        } else if (text == 'country_code') {
                            country_code = parser.getIntegerValue();
                        } else if (text == 'number') {
                            number_Z = parser.getText();
                        } else if (text == 'description') {
                            description = parser.getText();
                        } else if (text == 'formatted') {
                            formatted = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Phones consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Links {
        public Integer contacts {get;set;} 
        public Boolean validated {get;set;} 
        public String source {get;set;} 
        public String url {get;set;} 

        public Links(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'contacts') {
                            contacts = parser.getIntegerValue();
                        } else if (text == 'validated') {
                            validated = parser.getBooleanValue();
                        } else if (text == 'source') {
                            source = parser.getText();
                        } else if (text == 'url') {
                            url = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Links consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Revenue {

        public Revenue(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        {
                            System.debug(LoggingLevel.WARN, 'Revenue consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public class Emails {
        public String grade {get;set;} 
        public String type_Z {get;set;} // in json: type
        public String email {get;set;} 

        public Emails(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'grade') {
                            grade = parser.getText();
                        } else if (text == 'type') {
                            type_Z = parser.getText();
                        } else if (text == 'email') {
                            email = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Emails consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }


    public static JSON2Apex parse(String json) {
        return new JSON2Apex(System.JSON.createParser(json));
    }
}

This class is generated using JSON2APEX online tool. So please change it as per your need.

thanks

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.