I'm getting an error

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

for the json

    "customersId": "22",
    "stockType": "CurrentStock",
    "products": [
            "identifier": "GN20",
            "stock": [
                    "locationId": "xxxx",
                    "locationType": "Samsung",
                    "quantityInStock": 100,
                    "salesUnit": {
                        "code": "INOX",
                        "name": "INOX"

Following is the apex code

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

System.debug('deserialize result'+m);
Map<String, Object> myMaps = new Map<String, Object>();
myMaps = (Map<String, Object>)m.get('products'); // getting error
System.debug('myMaps:- ' + myMaps);
List<Object> myMapObjects = (List<Object>) myMaps.get('stock');

After this, I want to iterate the locationId and locationtype from json and display it in vf page

  • 1
    You should replace the type of myMapObjects from List<Object> to a Map<String,Object> (similar to what you have on the myMaps variable). – user1067017 Jun 8 at 15:12
  • 2
    This is one of the situations where the error message tells you exactly what the problem is, but you may need some more conceptual framework to understand why. See our canonical QA, How do I get started working with JSON in Apex? – David Reed Jun 8 at 15:18
  • @user1067017 still i get the same error. – Vishal J Jun 8 at 15:18
  • @DavidCheng no still it has the same error – Vishal J Jun 8 at 15:18

Browse other questions tagged or ask your own question.