0

I’m trying to make a webcallout to pull data from the webservice and insert data from the JSON response into my custom object. After making the callout I’m getting an error of System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set. I'm getting an error on the line:

List<JSON2APex.Om> lj2a = (List<JSON2APex.Om>)System.JSON.deserialize(jsonResponse, List<JSON2APex.Om>.class);

How can I fix this error? Thank you.

My Class:

public class ListOutbound3 {

    public static void getListOutBoundRecords(DateTime endDate ){     

        String jsonResponse = (JSON.serializePretty(res));
        List<JSON2APex.Om> lj2a = (List<JSON2APex.Om>)System.JSON.deserialize(jsonResponse, List<JSON2APex.Om>.class);

        for(JSON2APex.Om j2a:  lj2a){
            Custom__c co = new Custom__c();
            co.Duration__c = j2a.dur;
            co.Concord_Account_ID__c = String.valueOf(j2a.id); 
            System.debug(co.Duration__c);

        }

        }

    }

Wrapper Class:

public class JSON2Apex {

    public List<String> TotalRecords_type_info;
    public Integer TotalRecords;
    public List<String> Success_type_info;
    public Boolean Success;
    public List<String> OutboundMessageList_type_info;
    public OutboundMessageList OutboundMessageList;
    public List<String> Message_type_info;
    public String Message;
    public List<String> field_order_type_info;
    public List<String> Code_type_info;
    public Integer Code;
    public List<String> apex_schema_type_info;

    public class OutboundMessageList {
        public List<String> om_type_info;
        public List<Om> om;
        public List<String> field_order_type_info;
        public List<String> apex_schema_type_info;
    }

    public class Om {
        public List<String> stat_x_type_info;
        public String stat_x;
        public List<String> id_type_info;
        public String id;
        public List<String> dur_type_info;
        public Integer dur;

    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

JSON Response:

{
  "TotalRecords_type_info" : [ "TotalRecords", "AccountManagementReportingWebservice", null, "1", "1", "false" ],
  "TotalRecords" : 65,
  "Success_type_info" : [ "Success", "AccountManagementReportingWebservice", null, "1", "1", "false" ],
  "Success" : true,
  "OutboundMessageList_type_info" : [ "OutboundMessageList", "AccountManagementReportingWebservice", null, "0", "1", "false" ],
  "OutboundMessageList" : {
    "om_type_info" : [ "om", "AccountManagementReportingWebservice", null, "0", "-1", "true" ],
    "om" : [ 
       {
      "stat_x_type_info" : [ "stat", "AccountManagementReportingWebservice", null, "0", "1", "false" ],
      "stat_x" : "Success",
      "id_type_info" : [ "id", "AccountManagementReportingWebservice", null, "0", "1", "false" ],
      "id" : "1233",
      "dur_type_info" : [ "dur", "AccountManagementReportingWebservice", null, "1", "1", "false" ],
      "dur" : 0
  } 
     ],
    "field_order_type_info" : [ "om" ],
    "apex_schema_type_info" : [ "AccountManagementReportingWebservice", "true", "false" ]
  },
  "Message_type_info" : [ "Message", "AccountManagementReportingWebservice", null, "0", "1", "false" ],
  "Message" : "Success",
  "field_order_type_info" : [ "Code", "Message", "OutboundMessageList", "Success", "TotalRecords" ],
  "Code_type_info" : [ "Code", "AccountManagementReportingWebservice", null, "1", "1", "false" ],
  "Code" : 0,
  "apex_schema_type_info" : [ "AccountManagementReportingWebservice", "true","false" ]
}

1 Answer 1

2

You're trying to deserialize something that's in the middle of where you are (the Om class). You'll need to deserialize the outside first, then drill in to that value:

Json2Apex result = Json2Apex.parse(jsonResponse);
for(Json2Apex.Om om: result.OutboundMessageList.om) {
    ...
1
  • Thank you, drilling into the inner classes solved it.
    – AL 600
    Commented Nov 22, 2019 at 13:45

You must log in to answer this question.

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