Monday, December 16, 2019

System.JSONException: Apex Type unsupported in JSON: Object

You will receive this error when your parsing/deserialize  the JSON responses either user the JSON classes or JSON Parser classes.Please read the below points
  • When you deserialize the response into specified class type and if that class is having any field with data type "Object" you will receive this error.
  • When you any JSONToApex Converter or JSONToC# we will end up with this issue.
  • Please replace all the fields with Object data type into String/Integer etc..
Sample JSON:


{
  "item": [
    {
      "CONTRACT": 1001117406,
      "SOLD_TO": 10081120,
      "PURCH_NO_C":,
      "STREET": "1290 Hamner Ave",
      "STR_SUPPL1": "",
      "STR_SUPPL2":,     
    }
  ]
}

After Coverting JSONToApex:

public class Item
{
    public integer CONTRACT { get; set; }
    public integer SOLD_TO { get; set; }
    public object PURCH_NO_C { get; set; }
    public string STREET { get; set; }
    public string STR_SUPPL1 { get; set; }
    public object STR_SUPPL2 { get; set; }
}

public class RootObject
{
    public List<Item> item { get; set; }
}



Deserialize the JSON:

JSONParser parser = JSON.createParser('sampleJsonResponse');
 RootObject pr= (RootObject)parser.readValueAs(RootObject.class); 
//Here you will receive an error because the RootObject.class is having fields (PURCH_NO_C,STR_SUPPL2)  of type Object .

WorkAround:
Please change the wrapper class fields data type from Object to appropriate types as integer,double,string etc

public class Item
{
    public integer CONTRACT { get; set; }
    public integer SOLD_TO { get; set; }
    public string PURCH_NO_C { get; set; }
    public string STREET { get; set; }
    public string STR_SUPPL1 { get; set; }
    public string STR_SUPPL2 { get; set; }
}

public class RootObject
{
    public List<Item> item { get; set; }
}

Hope this helps you..Enjoy..!


No comments:

Post a Comment