I have a requirement where the data from external system is fetched and upserted back in Salesforce.

below is my apex class:

public class RESTClass {
    public static void RESTClass() {   
        String jsonbody = '{ ' +
            '"username": "<username>",' +
            '"password": "<password>"' +
            '}' ;
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint('<user auth endpoint>');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        String response = res.getBody();
        UserAuthJSONParser jsondes = (UserAuthJSONParser) JSON.deserialize(response, UserAuthJSONParser.class);
        HttpRequest policyreq = new HttpRequest();
        policyreq.setEndpoint('<main endpoint>'); 
        policyreq.setHeader('Authorization', 'Bearer ' + jsondes.value.token);
        Http policyhttp = new Http();
        HTTPResponse policyres = policyhttp.send(policyreq);
        String policyresponse = policyres.getBody();
        JsonParser objJsonParser = (JsonParser) JSON.deserialize(policyresponse, JsonParser.class);
        JsonParser.cls_value clsValue = objJsonParser.value;
        Map<String, JsonParser.cls_data> clsDataMap = new Map<String, JsonParser.cls_data>();
        for(JsonParser.cls_data objClsData: clsValue.data){
            clsDataMap.put(objClsData.id, objClsData);
        list<policy__c> updatelist = new list<policy__c>();
        for (String eachIdFromMap : clsDataMap.keySet()){
            policy__c policy = new policy__c(
                unique_id__c = clsDataMap.get(eachIdFromMap).id,
                agent_id__c = clsDataMap.get(eachIdFromMap).agentId);
        upsert updatelist unique_id__c;

When I run the above class, I can able to see the output but on few occassions, I am getting following error

System.JSONException: No content to map to Object due to end of input

Can anyone please let me know if I am missing anything from my class which is causing this error.


  • if at all possible, Json.deserialize into an ApexType rather than use JsonParser and this will avoid these issues – cropredy Nov 25 '20 at 16:59

In policyresponse you might be getting an empty string '' or a whitespace string ' '.

You can do a anonymous apex and check with this code:

JsonParser objJsonParser = (JsonParser) JSON.deserialize('', JsonParser.class);


JsonParser objJsonParser = (JsonParser) JSON.deserialize(' ', JsonParser.class);

Do proper String null, empty and Blank check before deserialize method. String class in Apex has a isNotBlank method which checks for all three. You can use it then do your deserialize logic.


  • thanks for the solution! – SFDCUser Nov 26 '20 at 8:06

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.