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
EmilienGuichardEmilienGuichard 

Apex rest callout - get return object data

Hi,

I am making an apex rest callout and I am getting a list of objects
 
if (response.getStatusCode() == 200) {
            List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'equipements' key as a list
            List<Object> equipements = (List<Object>) results;
            System.debug('Received the following equipements:');
            for (Object equipement : equipements) {
                System.debug(equipement.get('cost'));

and I am getting an error "Method does not exist or incorrect signature: [Object].get(String)" when I try to access an ojbect field.

Here is an exemple of an object :
 
{_id=55d66226726b611100aaf741, cost=5000, lifespan=120, maintenanceperiod=365, name=Generator 1000 kW, quantity=5, replacement=false, sku=100003}
What is the right way to do it ?

Thanks a lot.

 
Best Answer chosen by EmilienGuichard
Rodrigo RESENDIZRodrigo RESENDIZ
Hi,
First of all your JSON body should have to the following format:
{"results":[{"_id":"55d66226726b611100aaf741", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"},{"_id":"55d66226726b611100aaf742", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"}]}

So JSON.deserializeUntyped method can parse it to a  Map<String,Object>. Then you can access to any of the elements within the map with the following:
string body = '{"results":[{"id":"55d66226726b611100aaf741", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"},'+
    '{"id":"55d66226726b611100aaf741", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"}]}';
Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(body);
//get from JSON body Map the 'results' list element
List<Object> resultslist = (List<Object>)results.get('results');
//iterate over the list of Maps
for(Object mapa:resultslist ){
    Map<String,Object> tempMap = (Map<String,Object>)mapa;
    //this is how you will Access to any element inside the Map
    // here you can add any ligic to process the data
    system.debug('id '+tempMap.get('id'));
}
Hope this help you.
 

All Answers

Rodrigo RESENDIZRodrigo RESENDIZ
Hi,
First of all your JSON body should have to the following format:
{"results":[{"_id":"55d66226726b611100aaf741", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"},{"_id":"55d66226726b611100aaf742", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"}]}

So JSON.deserializeUntyped method can parse it to a  Map<String,Object>. Then you can access to any of the elements within the map with the following:
string body = '{"results":[{"id":"55d66226726b611100aaf741", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"},'+
    '{"id":"55d66226726b611100aaf741", "cost":"5000", "lifespan":"120", "maintenanceperiod":"365", "name":"Generator 1000 kW", "quantity":"5", "replacement":"false", "sku":"100003"}]}';
Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(body);
//get from JSON body Map the 'results' list element
List<Object> resultslist = (List<Object>)results.get('results');
//iterate over the list of Maps
for(Object mapa:resultslist ){
    Map<String,Object> tempMap = (Map<String,Object>)mapa;
    //this is how you will Access to any element inside the Map
    // here you can add any ligic to process the data
    system.debug('id '+tempMap.get('id'));
}
Hope this help you.
 
This was selected as the best answer
EmilienGuichardEmilienGuichard
Thanks a lot !