function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kumar_arunkumar_arun 

Visualforce Error: System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set

Hi All,

I am getting 'System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set ' exception while parsing json, Please help

Thanks.
public class JSONWrapper{
   
    public cls_result[] result{get;set;}
    class cls_result {
        public String parent{get;set;}   //
        public String made_sla{get;set;} //true
        public String caused_by{get;set;}    //
        public String watch_list{get;set;}   //
        public String upon_reject{get;set;}  //cancel
        public String sys_updated_on{get;set;}   //2017-06-20 07:52:14
        public String child_incidents{get;set;}  //0
        public String hold_reason{get;set;}  //
        public String approval_history{get;set;} //
        public String number1{get;set;}   //5002800000m9AD4AAM
        public String resolved_by{get;set;}  //
        public String sys_updated_by{get;set;}   //admin
        public cls_opened_by opened_by{get;set;}
        public String user_input{get;set;}   //
        public String sys_created_on{get;set;}   //2017-06-20 07:52:14
        public cls_sys_domain sys_domain{get;set;}
        public String state{get;set;}    //1
        public String sys_created_by{get;set;}   //admin
        public String knowledge{get;set;}    //false
        public String order{get;set;}    //
        public String calendar_stc{get;set;} //
        public String closed_at{get;set;}    //
        public String cmdb_ci{get;set;}  //
        public String delivery_plan{get;set;}    //
        public String impact{get;set;}   //3
        public String active{get;set;}   //true
        public String work_notes_list{get;set;}  //
        public String business_service{get;set;} //
        public String priority{get;set;} //5
        public String sys_domain_path{get;set;}  ///
        public String rfc{get;set;}  //
        public String time_worked{get;set;}  //
        public String expected_start{get;set;}   //
        public String opened_at{get;set;}    //2017-06-20 07:52:14
        public String business_duration{get;set;}    //
        public String group_list{get;set;}   //
        public String work_end{get;set;} //
        public cls_caller_id caller_id{get;set;}
        public String resolved_at{get;set;}  //
        public String approval_set{get;set;} //
        public String subcategory{get;set;}  //
        public String work_notes{get;set;}   //
        public String short_description{get;set;}    //This is test incident created from salesforce CRM
        public String close_code{get;set;}   //
        public String correlation_display{get;set;}  //
        public String delivery_task{get;set;}    //
        public String work_start{get;set;}   //
        public String assignment_group{get;set;} //
        public String additional_assignee_list{get;set;} //
        public String business_stc{get;set;} //
        public String description{get;set;}  //
        public String calendar_duration{get;set;}    //
        public String close_notes{get;set;}  //
        public String notify{get;set;}   //1
        public String sys_class_name{get;set;}   //incident
        public String closed_by{get;set;}    //
        public String follow_up{get;set;}    //
        public String parent_incident{get;set;}  //
        public String sys_id{get;set;}   //b2472d10db23320085fe72ffbf961960
        public String contact_type{get;set;} //
        public String incident_state{get;set;}   //1
        public String urgency{get;set;}  //3
        public String problem_id{get;set;}   //
        public cls_company company{get;set;}
        public String reassignment_count{get;set;}   //0
        public String activity_due{get;set;} //
        public String assigned_to{get;set;}  //
        public String severity{get;set;} //3
        public String comments{get;set;} //
        public String approval{get;set;} //not requested
        public String sla_due{get;set;}  //
        public String comments_and_work_notes{get;set;}  //
        public String due_date{get;set;} //
        public String sys_mod_count{get;set;}    //0
        public String reopen_count{get;set;} //0
        public String sys_tags{get;set;} //
        public String escalation{get;set;}   //0
        public String upon_approval{get;set;}    //proceed
        public String correlation_id{get;set;}   //
        public String location{get;set;} //
        public String category{get;set;} //inquiry
    }
    class cls_opened_by {
        public String link{get;set;} 
        public String value{get;set;}  
    }
    class cls_sys_domain {
        public String link{get;set;} 
        public String value{get;set;}    
    }
    class cls_caller_id {
        public String link{get;set;} 
        public String value{get;set;}   
    }
    class cls_company {
        public String link{get;set;} 
        public String value{get;set;}    
    }
    public List<JSONWrapper> parse(String json){
        return (List<JSONWrapper>) System.JSON.deserialize(json, List<JSONWrapper>.class);
        
        
    }

    
}
 
public class ServiceNowCntrl {
    
    public string bodyText{get;set;}
    public ServiceNowCntrl (){
    doInit();
    }
    public void doInit(){
           
        try{
        
        string user='xxx';
        
        string password='xxxxxxxx';
        
        HttpRequest request = new HttpRequest();

        request.setEndpoint('https://dev18687.service-now.com/api/now/table/incident?sysparm_limit=10');

        Blob auth = Blob.valueOf(user+':'+password);

        System.debug('Auth: '+ auth);

        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(auth);
        
        request.setMethod('GET');
 
        request.setHeader('Authorization', authorizationHeader);
       
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        
            Http http = new Http();
            HttpResponse res = new HttpResponse();

            res = http.send(request);
            system.debug(res.getBody());
            bodyText=res.getBody();
            
           JSONWrapper jw=new JSONWrapper();
           list<JSONWrapper> jwList=new List<JSONWrapper>();
           jwList=jw.parse(bodyText);

        }catch(System.CalloutException e){
            System.debug('Erro: '+ e.getMessage());
        }
        
    }
    
}

 
bainesybainesy
Your parser is expecting a list of objects as the return type which implies the JSON you are parsing should begin with an object array which begins with '['. Can you post example JSON you expect to parse?
kumar_arunkumar_arun
Hi bainesy, Thanks for reply, JSON I am trying to parse is given below.
{
    "result": [
        {
            "parent": "",
            "made_sla": "true",
            "caused_by": "",
            "watch_list": "",
            "upon_reject": "cancel",
            "sys_updated_on": "2017-06-20 07:52:14",
            "child_incidents": "0",
            "hold_reason": "",
            "approval_history": "",
            "number": "5002800000m9AD4AAM",
            "resolved_by": "",
            "sys_updated_by": "admin",
            "opened_by": {
                "link": "https://dev17687.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441",
                "value": "6816f79cc0a8016401c5a33be048e441"
            },
            "user_input": "",
            "sys_created_on": "2017-06-20 07:52:14",
            "sys_domain": {
                "link": "https://dev17687.service-now.com/api/now/table/sys_user_group/global",
                "value": "global"
            },
            "state": "1",
            "sys_created_by": "admin",
            "knowledge": "false",
            "order": "",
            "calendar_stc": "",
            "closed_at": "",
            "cmdb_ci": "",
            "delivery_plan": "",
            "impact": "3",
            "active": "true",
            "work_notes_list": "",
            "business_service": "",
            "priority": "5",
            "sys_domain_path": "/",
            "rfc": "",
            "time_worked": "",
            "expected_start": "",
            "opened_at": "2017-06-20 07:52:14",
            "business_duration": "",
            "group_list": "",
            "work_end": "",
            "caller_id": {
                "link": "https://dev17687.service-now.com/api/now/table/sys_user/be826abf03710200044e0bfc8bcbe5d1c",
                "value": ""
            },
            "resolved_at": "",
            "approval_set": "",
            "subcategory": "",
            "work_notes": "",
            "short_description": "This is test incident created from salesforce CRM",
            "close_code": "",
            "correlation_display": "",
            "delivery_task": "",
            "work_start": "",
            "assignment_group": "",
            "additional_assignee_list": "",
            "business_stc": "",
            "description": "",
            "calendar_duration": "",
            "close_notes": "",
            "notify": "1",
            "sys_class_name": "incident",
            "closed_by": "",
            "follow_up": "",
            "parent_incident": "",
            "sys_id": "b2472d10db23320085fe72ffbf961960",
            "contact_type": "",
            "incident_state": "1",
            "urgency": "3",
            "problem_id": "",
            "company": {
                "link": "https://dev17687.service-now.com/api/now/table/core_company/a66b1f88b03710200044e0bfc8bcbe5d08",
                "value": ""
            },
            "reassignment_count": "0",
            "activity_due": "",
            "assigned_to": "",
            "severity": "3",
            "comments": "",
            "approval": "not requested",
            "sla_due": "",
            "comments_and_work_notes": "",
            "due_date": "",
            "sys_mod_count": "0",
            "reopen_count": "0",
            "sys_tags": "",
            "escalation": "0",
            "upon_approval": "proceed",
            "correlation_id": "",
            "location": "",
            "category": "inquiry"
        }
    ]
}
bainesybainesy
Try this for your JSONWrapper class:
public class JSONWrapper{
   
    public cls_result result{get;set;}
    class cls_result {
        public String parent{get;set;}   //
        public String made_sla{get;set;} //true
        public String caused_by{get;set;}    //
        public String watch_list{get;set;}   //
        public String upon_reject{get;set;}  //cancel
        public String sys_updated_on{get;set;}   //2017-06-20 07:52:14
        public String child_incidents{get;set;}  //0
        public String hold_reason{get;set;}  //
        public String approval_history{get;set;} //
        public String number1{get;set;}   //5002800000m9AD4AAM
        public String resolved_by{get;set;}  //
        public String sys_updated_by{get;set;}   //admin
        public cls_opened_by opened_by{get;set;}
        public String user_input{get;set;}   //
        public String sys_created_on{get;set;}   //2017-06-20 07:52:14
        public cls_sys_domain sys_domain{get;set;}
        public String state{get;set;}    //1
        public String sys_created_by{get;set;}   //admin
        public String knowledge{get;set;}    //false
        public String order{get;set;}    //
        public String calendar_stc{get;set;} //
        public String closed_at{get;set;}    //
        public String cmdb_ci{get;set;}  //
        public String delivery_plan{get;set;}    //
        public String impact{get;set;}   //3
        public String active{get;set;}   //true
        public String work_notes_list{get;set;}  //
        public String business_service{get;set;} //
        public String priority{get;set;} //5
        public String sys_domain_path{get;set;}  ///
        public String rfc{get;set;}  //
        public String time_worked{get;set;}  //
        public String expected_start{get;set;}   //
        public String opened_at{get;set;}    //2017-06-20 07:52:14
        public String business_duration{get;set;}    //
        public String group_list{get;set;}   //
        public String work_end{get;set;} //
        public cls_caller_id caller_id{get;set;}
        public String resolved_at{get;set;}  //
        public String approval_set{get;set;} //
        public String subcategory{get;set;}  //
        public String work_notes{get;set;}   //
        public String short_description{get;set;}    //This is test incident created from salesforce CRM
        public String close_code{get;set;}   //
        public String correlation_display{get;set;}  //
        public String delivery_task{get;set;}    //
        public String work_start{get;set;}   //
        public String assignment_group{get;set;} //
        public String additional_assignee_list{get;set;} //
        public String business_stc{get;set;} //
        public String description{get;set;}  //
        public String calendar_duration{get;set;}    //
        public String close_notes{get;set;}  //
        public String notify{get;set;}   //1
        public String sys_class_name{get;set;}   //incident
        public String closed_by{get;set;}    //
        public String follow_up{get;set;}    //
        public String parent_incident{get;set;}  //
        public String sys_id{get;set;}   //b2472d10db23320085fe72ffbf961960
        public String contact_type{get;set;} //
        public String incident_state{get;set;}   //1
        public String urgency{get;set;}  //3
        public String problem_id{get;set;}   //
        public cls_company company{get;set;}
        public String reassignment_count{get;set;}   //0
        public String activity_due{get;set;} //
        public String assigned_to{get;set;}  //
        public String severity{get;set;} //3
        public String comments{get;set;} //
        public String approval{get;set;} //not requested
        public String sla_due{get;set;}  //
        public String comments_and_work_notes{get;set;}  //
        public String due_date{get;set;} //
        public String sys_mod_count{get;set;}    //0
        public String reopen_count{get;set;} //0
        public String sys_tags{get;set;} //
        public String escalation{get;set;}   //0
        public String upon_approval{get;set;}    //proceed
        public String correlation_id{get;set;}   //
        public String location{get;set;} //
        public String category{get;set;} //inquiry
    }
    class cls_opened_by {
        public String link{get;set;} 
        public String value{get;set;}  
    }
    class cls_sys_domain {
        public String link{get;set;} 
        public String value{get;set;}    
    }
    class cls_caller_id {
        public String link{get;set;} 
        public String value{get;set;}   
    }
    class cls_company {
        public String link{get;set;} 
        public String value{get;set;}    
    }
    public JSONWrapper parse(String json){
        return (JSONWrapper) System.JSON.deserialize(json, JSONWrapper.class);
        
        
    }

    
}

and this for your parser:
public class ServiceNowCntrl {
    
    public string bodyText{get;set;}
    public ServiceNowCntrl (){
    doInit();
    }
    public void doInit(){
           
        try{
        
        string user='xxx';
        
        string password='xxxxxxxx';
        
        HttpRequest request = new HttpRequest();

        request.setEndpoint('https://dev18687.service-now.com/api/now/table/incident?sysparm_limit=10');

        Blob auth = Blob.valueOf(user+':'+password);

        System.debug('Auth: '+ auth);

        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(auth);
        
        request.setMethod('GET');
 
        request.setHeader('Authorization', authorizationHeader);
       
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        
            Http http = new Http();
            HttpResponse res = new HttpResponse();

            res = http.send(request);
            system.debug(res.getBody());
            bodyText=res.getBody();
            
           JSONWrapper jw=new JSONWrapper();
           JSONWrapper jwList=new JSONWrapper();
           jwList=jw.parse(bodyText);

        }catch(System.CalloutException e){
            System.debug('Erro: '+ e.getMessage());
        }
        
    }
    
}