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
Gabriel MocelinGabriel Mocelin 

REST API: Invalid conversion from runtime type List<ANY> to Map<String,ANY>

Hi!

I'm trying to make an API call to populate some fields on a visual force page, but I'm catching this problem, does anyone have any idea what it could be?

System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> at line 31.
 
public with sharing class pocAuthorizationManagement {
	
    public String email{get;set;}
    public String id_json{get;set;}
    public String inserted_at{get;set;}
    public String mobile_phone{get;set;}
    public String person_id{get;set;}
    public String term_id{get;set;}
    public String ip{get;set;}
    public String user_agent{get;set;}
    
    
    public pocAuthorizationManagement(ApexPages.StandardController stdController){
      
        Lead lead = (Lead)stdController.getRecord();
        lead = [SELECT Id, CPFTxt__c FROM Lead WHERE Id =: lead.Id];
        
        String leadCPF = lead.CPFTxt__c;
        
        String requestEndPoint = 'my-end-point;
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(requestEndPoint);
        request.setMethod('GET');
        HttpResponse response =  http.send(request);
        
        if(response.getStatusCode() ==  200){
            Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
            System.debug(results);
            Map<String, Object> dataResults = (Map<String, Object>)(results.get('data'));
            email = String.valueOf(dataResults.get('email'));
            id_json = String.valueOf(dataResults.get('id'));
            inserted_at = String.valueOf(dataResults.get('inserted_at'));
            person_id = String.valueOf(dataResults.get('person_id'));
            mobile_phone = String.valueOf(dataResults.get('mobile_phone'));
            term_id = String.valueOf(dataResults.get('term_id'));
            
            Map<String, Object> sessionResults = (Map<String, Object>)(dataResults.get('session_metadata'));
            ip = String.valueOf(dataResults.get('ip'));
            user_agent = String.valueOf(dataResults.get('user_agent'));
            
        } else {
        	ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Algo de errado não está certo');
            ApexPages.addMessage(msg);
        }
   
    }

}
Exemple of my JSON.
{
   "data":[
      {
         "email":null,
         "id":"e729f678-8793-4793-a50a-2ad85b221139",
         "inserted_at":"2019-05-10T18:16:46",
         "mobile_phone":null,
         "person_id":"5557e08f-b569-47b9-993c-29fb85830724",
         "session_metadata":{
            "ip":"xxx",
            "user_agent":"{\"OS\": \"Android\", \"ip\": \"xxx\", \"Vendor\": \"Google Inc.\", \"AppName\": \"Netscape\", \"Browser\": \"Chrome\", \"OSVersion\": 9, \"Plataform\": \"Linux armv8l\", \"UserAgent\": \"Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"AppVersion\": \"5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"BrowserVersion\": 63.03239111}"
         },
         "term_id":"a174ac7e-be49-4ab2-8539-83e85367b866"
      },
      {
         "email":null,
         "id":"c7cec531-4057-4684-a6b9-6422e5138b9d",
         "inserted_at":"2019-05-10T18:16:46",
         "mobile_phone":null,
         "person_id":"5557e08f-b569-47b9-993c-29fb85830724",
         "session_metadata":{
            "ip":"xxx",
            "user_agent":"{\"OS\": \"Android\", \"ip\": \"xxx\", \"Vendor\": \"Google Inc.\", \"AppName\": \"Netscape\", \"Browser\": \"Chrome\", \"OSVersion\": 9, \"Plataform\": \"Linux armv8l\", \"UserAgent\": \"Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"AppVersion\": \"5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"BrowserVersion\": 63.03239111}"
         },
         "term_id":"a174ac7e-be49-4ab2-8539-83e85367b866"
      }
   ]
}

My debug at line 30:
{data=({email=null, id=e729f678-8793-4793-a50a-2ad85b221139, inserted_at=2019-05-10T18:16:46, mobile_phone=null, person_id=5557e08f-b569-47b9-993c-29fb85830724, session_metadata={ip=xxx, user_agent={"OS": "Android", "ip": "xxx", "Vendor": "Google Inc.", "AppName": "Netscape", "Browser": "Chrome", "OSVersion": 9, "Plataform": "Linux
 Thanks for your help!
Best Answer chosen by Gabriel Mocelin
Philippe UyttendaelePhilippe Uyttendaele
@Gabriel,
your problem is that your data is an Array of object and not yet and object itself.
thus you should say 
List<Object> dataResults = (List<Object>)(results.get('data'));

then perform a loop like :
for (Object o : dataResults)
{
     Map<String, object> aDataResultObject = (Map<String,Object) o;
     // then all your logic here
}

you can't skip the step of first parsing the list then doing the loop.

Phil
 

All Answers

Agustin BAgustin B
Hi, you are casting the list thats the issue on that line.
Change: Map<String, Object> dataResults = (Map<String, Object>)(results.get('data'));
For: Map<String, Object> dataResults = new Map<String, Object>(results.get('data'));

if this solves your issue please mark this answer as correct. It may help others
Gabriel MocelinGabriel Mocelin
Hi Agustin,

When i change, i get this error, and cant save the file

Invalid initializer type Object found for Map<String,Object>: expected a Map with the same key and value types, or a valid SObject List
Agustin BAgustin B
Hi Gabriel, does every item in your Object list have an id field? Because the map will try to use the id as the key.
which is the structure of every object in that list?
Gabriel MocelinGabriel Mocelin
Yes, Agustin like my JSON example that I posted above, may contain more blocks with information (email, id, person_id, mobile_phone, ...)
Philippe UyttendaelePhilippe Uyttendaele
@Gabriel,
your problem is that your data is an Array of object and not yet and object itself.
thus you should say 
List<Object> dataResults = (List<Object>)(results.get('data'));

then perform a loop like :
for (Object o : dataResults)
{
     Map<String, object> aDataResultObject = (Map<String,Object) o;
     // then all your logic here
}

you can't skip the step of first parsing the list then doing the loop.

Phil
 
This was selected as the best answer
Gabriel MocelinGabriel Mocelin
Hi Phil,

Worked, thank you!

Is bringing the last node, what if I wanted to take the first one for example?