1

I am currently working on an integration with other systems and I have the JSON coming back in the below format.

{
    "searchDocumentsResponse": {
        "documentsIndexInfo": [
            {
                "contentElementsInfo": {
                    "fileName": "test.pdf",
                    "fileSize": 1010694,
                    "indexNumber": 0,
                    "mimeType": "application/pdf"
                },
                "guid": "{123456789-1234-1234-1234-92393300000}",
                "majorVersion": 1,
                "minorVersion": 0,
                "objectStore": "{09876543212-1234-4B53-8CC5-469444E92456}",
                "pathName": "",
                "properties": [
                    {
                        "multiValue": false,
                        "name": "Document_Type",
                        "type": "STRING",
                        "value": "TEST"
                    },
                    {
                        "multiValue": false,
                        "name": "ScanTMSTMP",
                        "type": "DATE",
                        "value": "5/16/1920"
                    },
                    {
                        "multiValue": false,
                        "name": "f_DocNumber",
                        "type": "STRING",
                        "value": ""
                    },
                    {
                        "multiValue": false,
                        "name": "MimeType",
                        "type": "STRING",
                        "value": "application/pdf"
                    },
                    {
                        "multiValue": false,
                        "name": "DateCreated",
                        "type": "DATE",
                        "value": "5/31/2018"
                    }
                ]

            }
         ],
        "numDocObjectsFound": 1,
        "numDocObjectsReturned": 1,
        "responseStatus": {
            "code": "OK",
            "message": ""
        },
        "startingDocObject": 0
    }
}

And I am trying to parse the response like below:

public class DocumentRetrievalResponse {

    public SearchDocumentsResponse searchDocumentsResponse {get;set;}
    public cecDocumentRetrievalResponse(){
    }

    public class SearchDocumentsResponse {
        public String numDocObjectsFound {get;set;}
        public String numDocObjectsReturned {get;set;}
        public ResponseStatus responseStatus {get;set;}
        public String startingDocObject {get;set;}
        public List<DocumentsIndexInfo> documentsIndexInfo {get;set;} {documentsIndexInfo = new List<DocumentsIndexInfo>();}
        public SearchDocumentsResponse(){
        }
    }

    public class ResponseStatus {
        public String code {get;set;}
        public String message{get;set;}
        public ResponseStatus(){

        }
    }

    public class DocumentsIndexInfo {
        public String guid{get;set;} {guid = null;}
        public Integer majorVersion{get;set;} {majorVersion = 0;}
        public Integer minorVersion{get;set;}{minorVersion = 0;}
        public String objectStore{get;set;}{objectStore = null;}
        public String pathName{get;set;} {pathName = null;}
        public ContentElementsInfo contentElementsInfo {get;set;}
        public List<Properties> properties{get;set;} {properties = new List<Properties>();} 

    }

    public class ContentElementsInfo {
        public String fileName{get;set;} {fileName = null;}
        public Integer fileSize{get;set;} {fileSize = 0;}
        public Integer indexNumber{get;set;} {indexNumber = 0;}
        public String mimeType{get;set;}{mimeType = null;}
    }


    public class Properties {
        public String name {get;set;}
        public String value {get;set;} 
        public String type {get;set;} 
        public String multiValue {get;set;} {multiValue = 'false';}
    }

}

And to deserialize, I have the below lines:

System.Type resultType = System.Type.forName('DocumentRetrievalResponse');
DocumentRetrievalResponse deserializeResults = 
    (DocumentRetrievalResponse)JSON.deserialize(result, resultType);

When I run it, I get the JSON Exception as below. I might be overthinking and missing something here. Any help on this is appreciated.

FATAL_ERROR System.JSONException: Expected DocumentRetrievalResponse.ContentElementsInfo but found [line:1, column:2444]

PS: if I comment the declaration public ContentElementsInfo contentElementsInfo {get;set;}, the deserialize happens fine but without ContentElementsInfo.

3
  • 1
    Unless its a typo, you have an invalid constructor here public cecDocumentRetrievalResponse(). Is this the only error you are getting? With a minimal code, if reproduced, I don't see an issue here.
    – Jayant Das
    May 8, 2019 at 20:56
  • Your model syntax is awkward, as you have multiple instance initializers (e.g. {documentsIndexInfo = new List<DocumentsIndexInfo>();}) in several of your class definitions. They do nothing in the context of deserialization and can simply be removed. In addition, the documentation recommends you reserve use of initializers for complex logic.
    – Adrian Larson
    May 8, 2019 at 20:59
  • N.B. attribute multiValue sb declared as Boolean; majorVersion and minorVersion as Integer
    – cropredy
    May 8, 2019 at 21:00

1 Answer 1

4

You seem to have a typo in your class.

cecDocumentRetrievalResponse()

should be DocumentRetrievalResponse()

If I use your same DocumentRetrievalResponse class, remove the cec and use your JSON I can run your lines and it works for me. I made no changes to your code.

String result = '{"searchDocumentsResponse": {"documentsIndexInfo": [{"contentElementsInfo": {"fileName": "test.pdf","fileSize": 1010694,"indexNumber": 0,"mimeType": "application/pdf"},"guid": "{123456789-1234-1234-1234-92393300000}","majorVersion": 1,"minorVersion": 0,"objectStore": "{09876543212-1234-4B53-8CC5-469444E92456}","pathName": "","properties": [{"multiValue": false,"name": "Document_Type","type": "STRING","value": "TEST"},{"multiValue": false,"name": "ScanTMSTMP","type": "DATE","value": "5/16/1920"},{"multiValue": false,"name": "f_DocNumber","type": "STRING","value": ""},{"multiValue": false,"name": "MimeType","type": "STRING","value": "application/pdf"},{"multiValue": false,"name": "DateCreated","type": "DATE","value": "5/31/2018"}]} ],"numDocObjectsFound": 1,"numDocObjectsReturned": 1,"responseStatus": {"code": "OK","message": ""},"startingDocObject": 0}}';
System.Type resultType = System.Type.forName('DocumentRetrievalResponse');

DocumentRetrievalResponse deserializeResults = (DocumentRetrievalResponse)JSON.deserialize(result, resultType); 
System.debug('deserializeResults ' + deserializeResults.searchDocumentsResponse.documentsIndexInfo[0].contentElementsInfo.fileName);
3
  • Thank you so much for the help Jayant Das, Adrian Larson, Cropredy and Tad. As everyone rightly mentioned there was a typo and also removed unnecessary initializers. For the JSON example above, I was able to deserialize successfully.
    – Rajesh
    May 9, 2019 at 15:24
  • When I was checking for few more responses, I noticed that the 'contentElementsInfo' comes back as the List of object (sometimes). When I checked with the team who is exposing the service, they mentioned unfortunately they cannot make it as List always as they are getting such response from somewhere else which I do not have much information.
    – Rajesh
    May 9, 2019 at 15:26
  • That being said, I will have to deserialize based on the type of the 'contentElementsInfo'. So, it could be something like below: "contentElementsInfo" : {.....} OR "contentElementsInfo" : [{.......}, {.....}]
    – Rajesh
    May 9, 2019 at 15:26

You must log in to answer this question.

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