1

I'm trying to deserialize a JSON response from an API.

Let's say the JSON looks like this:

{
    "pages": 3,
    "this_page": 1,
    "products": [
        {
            "id": "1",
            "quantity": 1,
            "name": "Potato",
            "description": "Yummy potato"
        },
        {
            "id": "2",
            "quantity": 6,
            "name": "Tomato",
            "description": "Ugly potato"
        }
   ]

}

I have a Product__c sObject with the fields Quantity__c, Name and External_ID__c

Now to parse the response into object I do the following:

global class FetchProducts{

  public static List<Product__c> getProducts(){

    ..
    ..

    HttpRequest req = new HttpRequest();
    String authorizationHeader = 'Basic ' + authHash;

    req.setEndpoint(url);

    Http http = new Http();
    HttpResponse res = http.send(req);

    String jsonResponse = res.getBody();

    // replace JSON properties with sObject fields for deserealization
    jsonResponse = jsonResponse.replace('"id":', '"External_ID__c":');
    jsonResponse = jsonResponse.replace('"quantity":', '"Quantity__c":');
    jsonResponse = jsonResponse.replace('"name":', '"Name":');

    // serialize 

    MainResponse mr = (MainResponse)JSON.deserialize(responseBody, MainResponse.class);

    return mr.products
  }


  class MainResponse{
    public String pages;
    public String this_page;
    public List<Product__c> products;

    public MainResponse(String p, String tp){
      this.pages = p;
      this.this_page = tp;
      this.products = new List<Product__c>();
    }
  }
}

My problem is that I'm getting the following error:

 System.JSONException: No such column 'description' on sobject of type Product__c

I don't understand why I'm getting this since that property should be ignored. The documentation from deserialize method states:

If the JSON content to parse contains attributes not present in the Apex type specified in the argument, such as a missing field or object, this method ignores these attributes and parses the rest of the JSON content

That description property should be ignored right?

1 Answer 1

2

That statement apply for Apex type. You are expecting it to a custom object structure/column. During deserialization json try to assign values to product__c which is not a Apex class, it is a sobject so in that case json value try to find a exact match of column for product__c instance but it has different structure then json keys and throw error. I believe that json statement applies to primitives and objects only, not for sobject.

4
  • Thanks @regalforce. If I use an Apex class for deserialization is there an loop-free way to convert the Apex object to sObject? I say loop-free because the response could have many objects and building a transformation loop could break execution script limits. Jul 5, 2014 at 2:55
  • 1
    why not just add a description__c field to your custom object Product__c?
    – cropredy
    Jul 6, 2014 at 0:54
  • @crop1645 I didn't want to add irrelevant information to my object but I think it's the only way. Jul 7, 2014 at 16:45
  • Yes, I agree - a hack if you don't want to parse individual JSON name/value pairs and explicitly move into sobject fields. You can hide the description__c field from the page layout and, do what I do some times, and make the field label something like Sysad: Description (ignorable) so when the field appears in sobject field lists (reports, for example), you are cued to ignore it.
    – cropredy
    Jul 7, 2014 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .