I am trying to get the status,# record processed etc details by hitting the bulk API endpoint - /services/async/39.0/job/75037000003BXtw/batch

In response I got the below JSON structure:


I used JSONtoApex tool to generate the required Class to parse the JSON:

public class BatchInfo {
    public Integer apexProcessingTime;
    public Integer apiActiveProcessingTime;
    public String createdDate;
    public String id;
    public String jobId;
    public Integer numberRecordsFailed;
    public Integer numberRecordsProcessed;
    public String state;
    public Object stateMessage;
    public String systemModstamp;
    public Integer totalProcessingTime;

As usual I use the below code to desearialize the JSON file:

List<BatchInfo> respJson =(List<BatchInfo>)JSON.deserialize(res2.getbody() , List<BatchInfo>.class);

I get an error saying - System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set

On doing some research I found a link which could resolve this issue - Deserialize JSON Array With Integer Index , but I get some other error now when I try to desearlize using the code in the link:

Map<String,Object> jsonMap = (Map<String,Object>)JSON.deserializeUntyped(res2.getbody());
String jsonSubset = JSON.serialize(jsonMap.get('batchInfo'));
system.debug('JsonSubset======>' + jsonSubset); // [{"totalProcessingTime":5447,"systemModstamp":"2017-07-26T23:51:37.000+0000","stateMessage":null,"state":"Completed","numberRecordsProcessed":500,"numberRecordsFailed":0,"jobId":"75037000003BXtwAAG","id":"751370000046EakAAE","createdDate":"2017-07-26T23:51:29.000+0000","apiActiveProcessingTime":4765,"apexProcessingTime":1993}]
Map<String,Object> jsonSubsetMap = (Map<String, Object>)JSON.deserializeUntyped(jsonSubset); // get below error at this line

Error - System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY>

Any suggestion? or Answer?


You don't quite have the right structure. You need an outer wrapper as well.

public class BulkApiResponse
    public List<BatchInfo> batchInfo;

Now you can deserialize the structure:

BulkApiResponse response = (BulkApiResponse)JSON.deserialize(payload, BulkApiResponse.class);

Note that you can always tell whether or not you can deserialize into a collection by whether or not the payload starts with a square bracket ([). If it starts with a curly bracket ({), you need to use a Map or Object as the type to deserialize into.

  • Cool. I guess the link I mentioned in my question is a different problem. My JSON only had primitive types in it when I converted it into Apex class. – SfdcBat Jul 27 '17 at 19:00
  • @SfdcBat It looks like you passed in just the inner part of the list to JSON2Apex. – Adrian Larson Jul 27 '17 at 19:00
  • I missed this part completely from jsontoapex class - public class JSON2Apex { public List<BatchInfo> batchInfo; } Thanks for pointing this out. – SfdcBat Jul 27 '17 at 19:03

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.