0

For this JSON When I tried to serialize using the class generated from json2Apex I am getting the error "Malformed JSON: Expected '{' at the beginning of object"

JSON:

{
       "controllerValues": {},
       "defaultValue": null,
       "eTag" : "",
       "url" : "",
       "values" : [
        {
            "attributes": null,
                "label": "PicklistValue1",
                "validFor": [],
                "value": "PicklistValue1"
        },
        {
            "attributes": null,
                "label": "PicklistValue2",
                "validFor": [],
                "value": "PicklistValue2"
        }
       ]
    }

Apex:

public class casePicklistItemWrapper {

    public ControllerValues controllerValues;
    public Object defaultValue;
    public String eTag;
    public String url;
    public List<Values> values;

    public class Values {
        public Object attributes;
        public String label;
        public List<ControllerValues> validFor;
        public String value;
    }

    public class ControllerValues {
    }

    public static casePicklistItemWrapper parse(String json) {
        return (casePicklistItemWrapper) System.JSON.deserialize(json, casePicklistItemWrapper.class);
    }
}

class logic to process the json result

List<selectOption> result = new List<selectOption>();
        string jsonResult = res.getBody();
        casePicklistItemWrapper arenaPLMItemObject = casePicklistItemWrapper.parse(jsonResult); 
        List<casePicklistItemWrapper.values> caseSelectedpicklistbyRt   = arenaPLMItemObject.values;

        for(casePicklistItemWrapper.values PickVal: caseSelectedpicklistbyRt){
            result.add(new SelectOption(String.valueOf(PickVal.value),String.valueOf(PickVal.label)));
        }
        return result;

Why do I get this error? How can I fix it?

  • What is the JSON? Is it in fact valid, well-formed, and having an object at the top level? – David Reed Jun 12 '19 at 12:12
  • the JSON is the output from the user interface API containing the picklists values specific to a record type – user69684 Jun 12 '19 at 12:16
  • Have you checked the contents of the jsonResult string variable to confirm is an actual valid JSON. It must start with the curly braces, otherwise it's not valid JSON. You could do a System.debug(res.getBody()); before assigning that to the variable and check the debug logs to see it's content. – Federico Giust Jun 12 '19 at 12:27
1

I suspect that because your "defaultValue" & "attributes" variables in your wrapper are of the Object type, you will need to specify them as objects in your JSON rather than set them to null:

{
   "controllerValues": {},
   "defaultValue": {},
   "eTag" : "",
   "url" : "",
   "values" : [
    {
        "attributes": {},
        "label": "PicklistValue1",
        "validFor": [],
        "value": "PicklistValue1"
    },
    {
        "attributes": {},
        "label": "PicklistValue2",
        "validFor": [],
        "value": "PicklistValue2"
    }
   ]
}
| improve this answer | |
  • If you check the JSON in the question with JSONLint it is a valid JSON. – Federico Giust Jun 12 '19 at 13:54
  • I did check it, but the fact it’s a valid JSON doesn’t mean Salesforce will successfully be able to deserialise it into your wrapper. :-) – Koen Wesselman Jun 12 '19 at 17:09

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.