2

How we can deserialized below response using Apex?

{"attributes":{"type":"Case","url":"/services/data/v37.0/sobjects/Case/XXXXXXX"},
"CaseNumber":"XXX",
"Subject":"My Testing",
"Status":"Working",
"Priority":"Medium",
"Id":"XXX"}

I used something like below gave me error:

public class CaseDetails {
    public String CaseNumber {get; set;}
    public String Subject {get; set;}
    public String Status {get; set;}
    public String Priority {get; set;}
    public String Id {get; set;}
    public List<Attributes> attributes{get;set;}

    public class Attributes{
        public String type {get;set;}
        public String url {get;set;}
    }
}

The error I am getting

FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
FATAL_ERROR Class.ApexRestController.getCaseDetailsById: line 44, column 1

My Controller logic

public List<CaseDetails> cases {get; set;}
cases = (List<CaseDetails>) JSON.deserialize(res.getBody(), List<CaseDetails>.class);
  • Are you sure about the data you are getting as JSON is correct? – itzmukeshy7 May 15 '17 at 18:12
4

Ashwani's answer is the way to go but....

To answer you question directly, your attributes are not a list, they are an object. Adjust your class as follows:

public class CaseDetails {
    public String CaseNumber {get; set;}
    public String Subject {get; set;}
    public String Status {get; set;}
    public String Priority {get; set;}
    public String Id {get; set;}
    public Attributes attributes{get;set;} //not a list, instance of class

    public class Attributes{
        public String type_Z {get;set;} //Reserved to spend with _Z
        public String url {get;set;}
    }
}

Important You have a reserved keyword in your JSON String - type. If there is only one then you can do something like this to deserialize:

(CaseDetails) JSON.deserialize(resp.getBody().replace('"type"','"type_Z"'), CaseDetails.class));

otherwise it could become unmanageable and you would want to use the parser provided by the JSON2Apex

A really good tool to use when you have JSON string you would like to gt into a class would be JSON2APEX

| improve this answer | |
  • Malformed JSON: Expected '[' at the beginning of List/Set Error is in expression '{!getCaseDetailsById}' in component <apex:commandButton> in page apexrestvfpage: Class.System.JSON.deserialize: line 15, column 1 Class.ApexRestController.getCaseDetailsById: line 44, column 1 – user4567570 May 15 '17 at 18:12
  • The JSON response is the actual response I am getting, but I see issue while displaying the data? – user4567570 May 15 '17 at 18:16
  • @user4567570 then you should ask a new question wth the issue you are facing while displaying the data on the VF page. – itzmukeshy7 May 15 '17 at 18:18
  • @user4567570 - Updated answer. Your JSON was not a list of objects so the casting was not correct – Eric May 15 '17 at 18:18
  • Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexObjectValueELAdapter – user4567570 May 15 '17 at 18:31
1

This looks like Case object. You can deserialize it by type casting and JSON's deserialize method as below:

String caseStr = '{"attributes":{"type":"Case","url":"/services/data/v37.0/sobjects/Case/XXXXXXX"},'+
'"CaseNumber":"XXX",'+
'"Subject":"My Testing",'+
'"Status":"Working",'+
'"Priority":"Medium",'+
'"Id":"XXX"}';

Case caseObj = (Case)JSON.deserialize(caseStr, Case.class);

Another way is refer JSON2Apex link. Paste the given JSON. You would get the constructed class as:

//
//Generated by AdminBooster
//

public class CaseDetail{
    public cls_attributes attributes;
    public String CaseNumber;   //XXX
    public String Subject;  //My Testing
    public String Status;   //Working
    public String Priority; //Medium
    public String Id;   //XXX
    class cls_attributes {
        public String type; //Case
        public String url;  ///services/data/v37.0/sobjects/Case/XXXXXXX
    }
    public static CaseDetail parse(String json){
        return (CaseDetail) System.JSON.deserialize(json, CaseDetail.class);
    }

    static testMethod void testParse() {
        String json=        '{"attributes":{"type":"Case","url":"/services/data/v37.0/sobjects/Case/XXXXXXX"},'+
        '"CaseNumber":"XXX",'+
        '"Subject":"My Testing",'+
        '"Status":"Working",'+
        '"Priority":"Medium",'+
        '"Id":"XXX"}';
        CaseDetail obj = parse(json);
        System.assert(obj != null);
    }
}
| improve this answer | |
  • How can I display the data in VF pages? – user4567570 May 15 '17 at 18:14
0

Basic Sample JSON Deserialization... Sample JSON Response

{
"access_token":"XYZ...",
"tokenType":"BearerToken",
"issuedAt":"1505752810855",
"expiresIn":"35999",
"clientID":"XYZ!@#$$$$$",
"org":"abccorp"
}

Controller

public class PBAuthRequestBean {
       public String access_token;
       public String tokenType;
       public String issuedAt;
       public String expiresIn;
       public String clientID;
       public String org;


}

// Easiest way would be https://www.adminbooster.com/tool/json2apex or http://json2apex.herokuapp.com/ for getting the apex

sample Deserialization which works for most of the JSON s


PBAuthRequestBean responseBean=new PBAuthRequestBean ();
    responseBean= (PBAuthRequestBean )JSON.deserialize(response.getBody(),PBAuthRequestBean.class);
    system.debug('$$$$ResponseBean$$$$'+responseBean.access_token);

similar way you can get responseBean.tokenType and others ...

Hope this helps for folks with learning how to serialize ..

| improve this answer | |

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.