Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to parse a complex JSON String using JSON.deserializeUntyped(). It parses the first level elements correctly but the second level elements come back as type ANY, even though I am expecting it to be another Map. Following is a sample JSON String.

    "totalResults": 2,

My Apex code is

Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(jsonStr);

List<Map<String, Object>> pricing = (List<Map<String, Object>>)resultsMap.get('pricing');

I get an error on the line 2.

Invalid conversion from runtime type LIST to LIST>

What am I doing wrong? Or what is correct way of getting the list of prices?

share|improve this question
up vote 12 down vote accepted

You can make the code work with an extra step:

String s = '{"totalResults": 2, "startIndex":0, "pricing":[{"price":10.80,"cost":9.22,"gp":12},{"price":5.50,"cost":4.00,"gp":24}]}';
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
List<Object> pricing = (List<Object>) m.get('pricing');
for (Object o : pricing) {
    Map<String, Object> p = (Map<String, Object>) o;
    System.debug('>>> ' + p);
share|improve this answer
this works. thanks @Keith. – Amit May 9 '14 at 14:27

In these situations I find it much more ... less frustrating to deserialize to a known type.

Given an exemplar JSON string, you can input it here: and this will generate classes and tests to deserialize the json. Very handy.

share|improve this answer
I don't want to create a concrete class for the JSON String. The reason being that the JSON response coming from external service is expected to change quite a few times in next few months. I am trying to keep things flexible in Apex so we don't have to make any changes in Apex classes whenever JSON changes. Designers can use the Map from deserialized JSON on the visualforce page. – Amit May 9 '14 at 13:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.