0
    String updateObjStr = [{"fields":{"Upside_Amount__c":"1000","Id":"a0N21000002l2e7EAA"}},
    {"fields":{"Upside_Amount__c":"2000","Id":"a0N21000002raT7EAI"}},
    {"fields":{"Upside_Amount__c":"300","Id":"a0N21000002raUFEAY"}}];



    List<Map<String, Object>> updateString = new List<Map<String, Object>>();
    Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(updateObjStr);
    for (Object instance : (List<Object>)deserialized.get('fields')){
       updateString.add((Map<String, Object>)instance);
    }

throwing error Invalid conversion from runtime type List to Map

1 Answer 1

1

Your JSON is not a key and map initially, it's array of key/value so while deserializing you must cast in List instead map.

String updateObjStr = '[{"fields":{"Upside_Amount__c":"1000","Id":"a0N21000002l2e7EAA"}},{"fields":{"Upside_Amount__c":"2000","Id":"a0N21000002raT7EAI"}},{"fields":{"Upside_Amount__c":"300","Id":"a0N21000002raUFEAY"}}]';

List<Object> deserialized = (List<Object>)JSON.deserializeUntyped(updateObjStr);
for(Object o: deserialized){
    Map<String, Object> obMap = (Map<String, Object>)o;
    system.debug(obMap.keySet());
    Object fieldsMap = obMap.get('fields');
    //you can cast into map and traverse this fieldmap object in same way as above
}
2
  • Thanks for help Ayub.. I want to update the existing records using "Id" in the above string.. Can you please help me with code?
    – Suresh
    Apr 27, 2020 at 10:06
  • When you will parse it, you will get the id. after that create a new instance of object and update it. Also, I would request to start another question if still does not help. Accept the current answer to solve the current issue.
    – Ayub
    Apr 27, 2020 at 10:26

You must log in to answer this question.

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