1

I am trying to parse the below response but im getting the following error FATAL_ERROR System.TypeException: Invalid conversion from runtime type Map<String,ANY> to Map<String,List> Can someone tell me how to parse it? I haven't used any wrapper class though but when i use the map.get I just get one value instead of a list of values.

//String jsonInput2='{"UserDetails":[{"ContactLastName":"TestAcc","ContactFirstName":"TheLandlord","IsActive":true,"ContactRecordTypeId":"0120C000001OpcIQAS","ContactId":"0030C00000TLabQQAT","AccountId":"0010C00000ToNO9QAN"}]}';
Map<string,list<object>> strObjMap= new Map<string,list<object>>();
Map<string,list<object>>  obj = (Map<string,list<object>>)JSON.deserializeUntyped(jsonInput);
system.debug('Obj data >>>'+obj.values().size());

1 Answer 1

3

The only types you can cast to when using deserializeUntyped are:

  • Object
  • Map<String, Object>
  • List<Object>
  • String
  • Boolean
  • Decimal
  • Integer
  • Double

And then, only if it is actually a compatible data type.

So, for example, your code might look like:

Map<String, Object> parsedJson = (Map<String, Object>)JSON.deserializeUntyped(jsonInput2);
List<Object> userDetails = (List<Object>)parsedJSON.get('UserDetails');
for(Object userDetail: userDetails) {
  Map<String, Object> detail = (Map<String, Object>)userDetail;
  System.debug(detail.get('ContactLastName'));
}

If you want to get that data type, you can use a specific shape, as follows:

Map<String, List<Object>> parsedJson = 
  (Map<String, List<Object>>)
  JSON.deserialize(jsonInput2, Map<String, List<Object>>.class);

Or even:

Map<String, List<Map<String, Object>>> parsedJson = 
  (Map<String, List<Map<String, Object>>>)
  JSON.deserialize(
    jsonInput2, 
    Map<String, List<Map<String, Object>>>.class
  );

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.