+ Start a Discussion
rahma salesforcerahma salesforce 

Cannot read HttpResponse.getBody() output

Hi,
I'm invoking a Rest Webservice from my developer org,
the issue is I can't read the HTTP Response as it is not proper json
this is the form of the response:
[  
   {  
      "attributes":{  
         "type":"Account",
         "url":"/services/data/v36.0/sobjects/Account/001580000054G7bAAE"
      },
      "Id":"001580000054G7bAAE",
      "Name":"357",
      "Phone":"1233800"
   },
   {  
      "attributes":{  
         "type":"Account",
         "url":"/services/data/v36.0/sobjects/Account/001580000054G7cAAE"
      },
      "Id":"001580000054G7cAAE",
      "Name":"358",
      "Phone":"1233801"
   },
   {  
      "attributes":{  
         "type":"Account",
         "url":"/services/data/v36.0/sobjects/Account/001580000054G7dAAE"
      },
      "Id":"001580000054G7dAAE",
      "Name":"359",
      "Phone":"1233802"
   },
   {  
      "attributes":{  
         "type":"Account",
         "url":"/services/data/v36.0/sobjects/Account/001580000054G7eAAE"
      },
      "Id":"001580000054G7eAAE",
      "Name":"360",
      "Phone":"1233803"
   },
   {  
      "attributes":{  
         "type":"Account",
         "url":"/services/data/v36.0/sobjects/Account/001580000054G7fAAE"
      },
      "Id":"001580000054G7fAAE",
      "Name":"361",
      "Phone":"1233804"
   }
]

I'm using  wrapper classes to extract the response as following:
public class Response{
        public List<Account> accounts{get;set;}
}
    
 public class Account{
        public List<Attribute> attributes;  
        public String Id;
        public String Name;
        public String Phone;
   }   
   public class Attribute{
        public String type;
        public String url;
        
   }
but when trying to deserialize the reponse as the code below
Response response = (Response) JSON.deserialize(jsonResponse, Response.class);
I have this error:
System.JSONException: Malformed JSON: Expected '{' at the beginning of object
Is this because the HTTP response is not json ? it is specified in the content type though,
how can I convert the httpresponse body to json please ?


 
Best Answer chosen by rahma salesforce
Vivek DVivek D
Your json is a list not a single object and it is of type account. Try this 
List<Account> acco = (List<Account>)JSON.deserialize(jsonResponse,List<Account>.class);

 

All Answers

Vivek DVivek D
Your json is a list not a single object and it is of type account. Try this 
List<Account> acco = (List<Account>)JSON.deserialize(jsonResponse,List<Account>.class);

 
This was selected as the best answer
rahma salesforcerahma salesforce
Thank you for your reply
but still have this error now :
Line: 15, Column: 1
System.JSONException: Expected List<BatchGetDataFromDev1.Attribute> but found { at [line:1, column:3]
rahma salesforcerahma salesforce
Hi I found the issue
the attributes is not a list and should be an object
public class Account{
        public Attribute attributes;  
        public String Id;
        public String Name;
        public String Phone;
    }