0

I have used the code below to deserialize the json but throwing an error.

Map<String,Object> root =(Map<String,Object>)JSON.deserializeUntyped(res.getBody()));

"Callout error Invalid conversion from runtime type List<ANY> to Map<String,ANY>"

I want to deserialize the JSON using an apex class to get the list of values?

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

1 Answer 1

2

This error message:

Callout error Invalid conversion from runtime type List to Map

suggests that the JSON in the body you are receiving is a list i.e. starts with [ and ends with ].

If the data is a list, change to e.g.:

List<Object> root = (List<Object>) JSON.deserializeUntyped(res.getBody());
5
  • I have used Json2 Apex to deserialize the above mentioned JSON but while parsing I'm getting the error "Malformed JSON: Expected '{' at the beginning of object"
    – user69684
    Jun 12, 2019 at 11:49
  • @user69684 Sounds like the same problem. The incoming JSON is an array of objects (that starts with [) at its root not an object (that starts with {) at its root. See e.g. json.org.
    – Keith C
    Jun 12, 2019 at 12:14
  • when I used this List<Object> root = (List<Object>) JSON.deserializeUntyped(res.getBody()); it is saying illegal assignment from map to list .I have used Json2Apex to desereialize but facing the same issue
    – user69684
    Jun 12, 2019 at 12:20
  • @user69684 If you have following code, that could be generating the error. To make your question clear, post the code that is doing the parsing and a unit test case that reproduces the error.
    – Keith C
    Jun 12, 2019 at 12:23

Not the answer you're looking for? Browse other questions tagged .