0

I'm trying to deserialize http json output . Getting an ERROR :System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set

 public static void syncBC(){
        HttpResponse respond = getrespond();

        if(respond.getStatusCode()==200){

                list<customerLedgerEntry> clist= new customerLedgerEntry().parse(respond.getbody());

        }
    }
    public static HttpResponse getrespond(){
        Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint ('callout:Bc_test/PBCLE');
    request.setHeader('Accept','*/*');
    request.setMethod('GET');
    request.setTimeout(40000);
        HttpResponse response = Http.send(request);
        return response;
    }

    public class customerLedgerEntry{
        public String Entry_No;
        public Date Posting_Date;
        public String Document_Type;
        public String Document_No;
        public String Customer_No;
        public Decimal Amount_LCY;
        public Decimal Amount;
        public Date Initial_Entry_Due_Date;

        public list<customerLedgerEntry> parse(String json){

            return(list<customerLedgerEntry>) System.JSON.deserialize(json, list<customerLedgerEntry>.class);

        }

    }

Here is the JSON output example:

       {"@odata.context":"https://api.businesscentral.dynamics.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "value":[{"@odata.etag":"W/\"xxxxxxxxxxxxxxxxxxxxxxx\"",
            "Entry_No":1,"Posting_Date":"2018-11-05","Entry_Type":"Initial Entry","Document_Type":"xxx",
"Document_No":"xxx","Customer_No":"13",
            "Initial_Entry_Global_Dim_1":"",
        "Initial_Entry_Global_Dim_2":"",
        "Currency_Code":"","Amount":-9,
        "Amount_LCY":-9,"Debit_Amount":0,"Debit_Amount_LCY":0,
            "Credit_Amount":9,
        "Credit_Amount_LCY":9,
        "Initial_Entry_Due_Date":"2018-11-05",
        "User_ID":"xxxx","xxx":"","Reason_Code":"","Unapplied":false,
            "Unapplied_by_Entry_No":0,
    "Cust_Ledger_Entry_No":xxx},

    {"@odata.etag":"W/\"xxxxxxxxxxxxxxxxxxxxxxx\"",
    "Entry_No":2,"Posting_Date":"2018-11-21",
            "Entry_Type":"Initial Entry","Document_Type":"xxx",
    "Document_No":"1234","Customer_No":"5423","Initial_Entry_Global_Dim_1":"",
            "Initial_Entry_Global_Dim_2":"","Currency_Code":"","Amount":-91234.84,"Amount_LCY":-91234.84,"Debit_Amount":0,"Debit_Amount_LCY":0,
            "Credit_Amount":91234.84,"Credit_Amount_LCY":91234.84,"Initial_Entry_Due_Date":"2018-11-21","User_ID":"NANCYR","xxx":"","Reason_Code":"",
            "Unapplied":false,"Unapplied_by_Entry_No":0,"Cust_Ledger_Entry_No":xxx}]}
  • Something that I've noticed - looking at the JSON it seems like it is malformed. The value for Cust_Ledger_Entry_No is xxx instead of an actual string like so "xxx". You can try validating the JSON here - jsonlint.com. – Arthlete Jun 9 at 5:39
0

First, you have to deserialized untyped as your JSON is not a list of customerLedgerEntry. Then you can deserialize typed.

I have removed some of the invalid values from your JSON, and its working fine now. For Demo, I have just kept one record. See this is how it works.

Map<String, Object> jsonMap =   (Map<String, Object>) JSON.deserializeUntyped('{ "@odata.context":"https://api.businesscentral.dynamics.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "value":[ { "Posting_Date":"2018-11-21", "Entry_Type":"Initial Entry", "Document_Type":"xxx", "Document_No":"1234", "Customer_No":"5423"} ] }');

list<object> entrylisttemp = (list<object>)jsonMap.get('value');
list<testDeserialize.customerLedgerEntry> entries = (list<testDeserialize.customerLedgerEntry>)System.JSON.deserialize(JSON.serialize(entrylisttemp), list<testDeserialize.customerLedgerEntry>.class);
System.debug(entries);
| improve this answer | |
  • Thankyou Rahul. Now I'm getting System.LimitException: Apex heap size too large: 22645046, I think i need to write a batch class for not hitting the heap size. – jagadeesh parasa Jun 9 at 5:48
  • what is the size of entry list ? is it so big? @jagadeeshparasa – rahul gawale Jun 9 at 6:02
  • Yes @Rahul the size of entry list is like 23MB – jagadeesh parasa Jun 9 at 6:07
  • @jagadeeshparasa then you need to fetch that in smaller chunks, and yes batch will be fine. Just make sure that you request data in smaller chunks – rahul gawale Jun 9 at 6:16
  • can you send me the sample code to fetch the data in smaller chunks @Rahul – jagadeesh parasa Jun 9 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.