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
Artem Artemenko 9Artem Artemenko 9 

How i can get data by key after i do Body.toString()

Hi, I`m junior in apex, I took data from static resource, then i want to get data by key from this String, but i dont know how.
//Get static resource            
StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'GetCityResource' LIMIT 1];
            String body = sr.Body.toString();
//here i want to take name value
{"coord":{"lon":27.57,"lat":53.9},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":20,"feels_like":19.51,"temp_min":20,"temp_max":20,"pressure":1013,"humidity":82},"visibility":10000,"wind":{"speed":4,"deg":50},"clouds":{"all":75},"dt":1592830206,"sys":{"type":1,"id":8939,"country":"USA","sunrise":1592789886,"sunset":1592851521},"timezone":10800,"id":625144,"name":"Atlanta","cod":200}

 
Best Answer chosen by Artem Artemenko 9
AnudeepAnudeep (Salesforce Developers) 
Hi Artem, 

You could loop through the keyset and grab whatever matches your criteria.
 
for (String key : someMap.keySet()) {
                        (key + " = " + someMap.get(key));
                }
Or try something like this:
 
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(
  RestContext.request.requestBody.toString()
);
Map<String, Object> traits = (Map<String, Object>)params.get('traits');
for(String key: traits.keySet()) {
  Object value = traits.get(key);
}

Let me know if this helps

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Artem, 

You could loop through the keyset and grab whatever matches your criteria.
 
for (String key : someMap.keySet()) {
                        (key + " = " + someMap.get(key));
                }
Or try something like this:
 
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(
  RestContext.request.requestBody.toString()
);
Map<String, Object> traits = (Map<String, Object>)params.get('traits');
for(String key: traits.keySet()) {
  Object value = traits.get(key);
}

Let me know if this helps

Anudeep
This was selected as the best answer
Artem Artemenko 9Artem Artemenko 9
Hi, Anudeep
Thank you, second variant pretty good, also read in this topic (https://developer.salesforce.com/forums/?id=9060G000000BhCbQAK) and complete this. My solution below.
StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'GetCityResource' LIMIT 1];
            String body = sr.Body.toString();
            Map<String,Object> weathMap = (Map<String,Object>)Json.deserializeUntyped(body);
            usrCity = String.valueOf(weathMap.get('name'));

 
AnudeepAnudeep (Salesforce Developers) 
Glad to know that you found the solution Artem. I was just about to point you to the deserializeUntyped(jsonString) example from the documentation. Thanks