0

After a callout, I am getting a JSON response as:

    {
  "totalSize": 1,
  "done": true,
  "records": [
    {
      "attributes": {
        "type": "Document__c",
        "url": "/services/data/v35.0/sobjects/Document__c/a0A02000000jFwOEAU"
      },
      "Id": "a0A02000000jFwOEAU",
      "Name": "js.pdf",
      "CreatedDate": "2022-06-21T12:13:20.000+0000",
      "Type__c": "General",
      "Fund__r": {
        "attributes": {
          "type": "Fund__c",
          "url": "/services/data/v35.0/sobjects/Fund__c/a0E02000000SzzuEAC"
        },
        "Name": "Fund 0"
      },
      "Investment_Vehicle_Name__c": null,
      "Document_Date__c": "2022-06-21",
      "Document_Date_String__c": "6/21/2022",
      "Portal_Document__c": true,
      "Attachments": {
        "totalSize": 1,
        "done": true,
        "records": [
          {
            "attributes": {
              "type": "Attachment",
              "url": "/services/data/v35.0/sobjects/Attachment/00P0200000CoRXHEA3"
            },
            "Id": "00P0200000CoRXHEA3",
            "Name": "js.pdf",
            "ContentType": "application/pdf",
            "BodyLength": 109244
          }
        ]
      }
    }
  ]
}

When I try deserializing the above response using:

 List<Document__c> documentForLP = (List<Document__c>) JSON.deserialize(res.getBody(),Document__c.class);

I am getting the error:

10:33:33:353 USER_DEBUG [1085]|DEBUG|Error: No field name specified on column for sobject of type Document__c

I am sure that Document__c object is available in the org. But not able to comprehend what the exact error message is trying to convey.

1
  • Minimal example that I could get to simulate error. JSON.deserialize('{"records": [] }',SomeObject__c.class): works but JSON.deserialize('{"records": [{}] }',SomeObject__c.class) errors. Something with records attributes and array with empty element.
    – metasync
    Sep 20, 2022 at 5:54

1 Answer 1

1

JSON for List<Document__c> would look like this:

[
    {
        "attributes": {
        "type": "Document__c",
        "url": "/services/data/v35.0/sobjects/Document__c/a0A02000000jFwOEAU"
    },
    "Id": "a0A02000000jFwOEAU",
    "Name": "js.pdf",
    ...

i.e. its root element would be an array [...] in JSON.

Your JSON is starting with some information related to breaking the response up into pieces. One way to deal with that is to define your own Apex class for that root information:

public class Root {
    public Integer totalSize;
    public Boolean done;
    public Document__c[] records;
}

and deserialize to that and take the documents from that:

Root root = (Root) JSON.deserialize(res.getBody(), Root.class);
Document__c[] documents = root.documents;

Note also that the https://json2apex.herokuapp.com/ tool can be very helpful when trying to figure out how to handle JSON.

You must log in to answer this question.

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