+ Start a Discussion
krishna 2619krishna 2619 

how to get string from deserializeUntyped json

Below is the Json response which am getting .I was able to get "tenantcode" code but based on Tenent code I need string value

Example : if I loop map with string "204" I need 10 value s Output 

string str = '{"10":{"name":"IP Auth","tenantcode":"204"},"11":{"name":"Registration","tenantcode":"205"},"13":{"name":"API Demo","tenantcode":"206"},"2":{"name":" Test","tenantcode":"200"},"29":{"name":"testing 2","tenantcode":"207"},"3":{"name":"Training ","tenantcode":"201"},"30":{"name":" Test222","tenantcode":"208"},"31":{"name":"Martin","tenantcode":"212"},"32":{"name":"API Demo Customer","tenantcode":"209"},"4":{"name":"Adrian ","tenantcode":"202"},"9":{"name":"Stephen","tenantcode":"203"}}';
  Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(str);
    for(object obj : resultsMap.values()){
         Map<String,Object> data = (Map<String,Object>)obj;
          system.debug('utlevel30description---'+(String)data.get('tenantcode'));
      }
 

iRKiRK
Hi,

I would suggest that you loop over the keys in the map and use it to access the data. This way you'll always keep track of what is the current key that you used to read the data. Like this:
 
String str = '{"10":{"name":"IP Auth","tenantcode":"204"},"11":{"name":"Registration","tenantcode":"205"},"13":{"name":"API Demo","tenantcode":"206"},"2":{"name":" Test","tenantcode":"200"},"29":{"name":"testing 2","tenantcode":"207"},"3":{"name":"Training ","tenantcode":"201"},"30":{"name":" Test222","tenantcode":"208"},"31":{"name":"Martin","tenantcode":"212"},"32":{"name":"API Demo Customer","tenantcode":"209"},"4":{"name":"Adrian ","tenantcode":"202"},"9":{"name":"Stephen","tenantcode":"203"}}';
Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(str);
for(String key : resultsMap.keySet()){
    Map<String,Object> data = (Map<String,Object>)resultsMap.get(key);
    if(data.get('tenantcode') == '204') {
        System.debug('Found tenantcode 204 with key ' + key); // Will print: Found tenantcode 204 with key 10
    }
}

Let me know if this was helpful,
iRK
krishna 2619krishna 2619

Hi IRk,

Its working as expected I have one question here how we are getting "tenantcode" here ?.  (Map<String,Object>)resultsMap what does this statement do?