function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
udayar_jayamudayar_jayam 

Convert JSON to MAP

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.
Khan AnasKhan Anas (Salesforce Developers) 
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
Raj VakatiRaj Vakati
Use this
 
String input = '[{"01u9000000Ag0bbAAB":"203","01u9000000Ag0bZAAR":"23","Id":"00k0w000003HeFhAAK"}]';
List<Object> result = (List<Object>)JSON.deserializeUntyped(input); 
Set<String> strRes = new Set<String>(); 

for(Object obj : result) { 
    Map<String,Object> map1 = (Map<String,Object>)obj; 
    for(String key : map1.keyset()) { 
		strRes.add('Key as "'+key+'" and value as "'+(String)map1.get(key)+'"');
    } 
}