[SalesForce] JSON.deserialize not ignoring unknown properties.

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?

Best Answer

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.