Skip to main content

I want to store key and value for the following json dynamically. 

Key as "01u9000000Ag0bbAAB" and value as "203"

String input = '[{"01u9000000Ag0bbAAB":"203","01u9000000Ag0bZAAR":"23","Id":"00k0w000003HeFhAAK"}]';

Please advise on this.
2 answers
  1. Mar 28, 2019, 1:46 PM
    Hi Udayar,

    Greetings to you!

    You need to use deserializeUntyped(jsonString) method that will deserialize the specified JSON string into collections of primitive data types.

    Please try below code:

    String input = '[{"01u9000000Ag0bbAAB":"203","01u9000000Ag0bZAAR":"23","Id":"00k0w000003HeFhAAK"}]';

    List<Object> result = (List<Object>)JSON.deserializeUntyped(input);

    for(Object obj : result) {

    Map<String,Object> map1 = (Map<String,Object>)obj;

    for(String key : map1.keyset()) {

    System.debug('Key -->> ' + key);

    System.debug('Value -->> ' + (String)map1.get(key));

    }

    }

    I hope it helps you.

    Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

    Thanks and Regards,

    Khan Anas
0/9000