I just followed a trailhead on callouts and I am trying to get some values from an endpoint. My debug shows me my status is ok but I get an error on line 17:

Callout error Invalid conversion from runtime type List to Map

Anonymouse window

Http http = new Http();
HttpRequest request = new HttpRequest();

request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-test-APIKEY', 'xxxxxxxxxxxxxx');

HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'agencies' key as a list
    List<Object> agencies = (List<Object>) results.get('agencies');
    System.debug('Received the following:');
    for (Object agency: agencies) {

My result should be similar to below

    "uid": "72352a55-5be4-496e-804a-fc3kl261f0a4",
    "name": "Agency 1"
    "uid": "62352a55-5487-496e-804a-fc3klf48f0a4",
    "name": "Agency 2"

Because you are getting List with Json, So you need to parse it into List

List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
for (Object agency: results) {
    System.debug((Map<String, Object>)agency); //Then you can parse it into key value pair
| improve this answer | |
WebService static boolean checkIfTLDExists(id contractID, String authProvider)
    //Id remoteTLDId;
    List<Contract> contractsToSend = [Select id, Status, StartDate,A_Label__c,String__c from Contract WHERE id = :contractID];
    String tldName ;
    if (contractsToSend.size() != 1) {
        return false;
    Contract contractToSend = contractsToSend[0];
    if(contractToSend.A_Label__c != null && contractToSend.A_Label__c.length() > 0)
        tldName = contractToSend.A_Label__c.toLowerCase();
        tldName = contractToSend.String__c.toLowerCase();
    Http http = new Http();
    HttpRequest getTLDRequest = new HttpRequest();
    //Reference OAuth2 NamedCredential for endpoint and generated authorization header
    // increasing timeout to 30 secs for httprequest.
    getTLDRequest.setHeader('Content-Type', 'application/json');
    HTTPResponse restld = http.send(getTLDRequest);
    list<String> tldIds=new List<String>();

    //List<Object> tldresults = (List<Object>) JSON.deserializeUntyped(restld.getBody());
    //for (Object tldresult: tldresults) {
    //  System.debug((Map<String, Object>)tldresult); //Then you can parse it into key value pair

    Map<String, Object> tldresults = (Map<String, Object>)JSON.deserializeUntyped(restld.getBody());
        List<Object> lstTLDs = (List<Object>)tldresults.get('records'); 
        // List<Object> lstTLDs = (List<Object>)JSON.deserializeUntyped(restld.getBody());
        for (Object onetld: lstTLDs) 
            Map<String, Object> tldAttributes= (Map<String, Object>)onetld;
            return true;
    return false;

I am getting error :- Callout error Invalid conversion from runtime type List to Map

please help

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.