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.
Best Answer
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
This class is generated using JSON2APEX online tool. So please change it as per your need.
thanks