String updateObjStr = [{"fields":{"Upside_Amount__c":"1000","Id":"a0N21000002l2e7EAA"}},

    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


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;
    Object fieldsMap = obMap.get('fields');
    //you can cast into map and traverse this fieldmap object in same way as above
  • 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 '20 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 '20 at 10:26

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.