0

I'm pretty new to Salesforce/Apex and I'm trying to extract the information I need from the HTTP API call I make to an external system (like Piwik.pro) and store in list or a map. I want to get all the available sites and their names through API call.

This is the code that successfully got me the needed information.

   public static Map<String, Object> getSiteIds()  
    {
            try{
                // MAKE THE API CALL
                HttpRequest req = new HttpRequest();
                req.setMethod('GET');
                req.setHeader('Content-Type','application/vnd.api+json');
                req.setHeader('Authorization','Bearer ' + AccessTokenGetSet.getSetToken);
                req.setEndpoint('https://example.piwik.pro/api/apps/v2');
                
                Http h = new Http();
                HTTPResponse res = h.send(req);
                
                String strResponse = res.getBody();
                if (res.getStatusCode() == 200){
                Map<String,Object> newMap = (Map<String, 
                Object>)JSON.deserializeUntyped(res.getBody());
                }
                // GETS THE DATA AND ITERATES THROUGH ALL INDIVIDUAL AVAILABLE SITES AND ITS AVAILABLE ATTRIBUTES
                    List<Object> data =  (List<Object>) results.get('data'); 
                    for (Object dataIte : data){
                        System.debug(dataIte);
                    }
return data;
            }
            else {
                return null;
            }
        }
        catch (exception ex){
            System.debug(ex.getMessage());
            return null;
        }
    }

The System.debug from this code is: https://prnt.sc/QxUKxNK4TQ3A

But I just want to get the names and ids of each individual site.

Also, I've made Postman call just to double check the structure of the JSON response and it looks something like this: https://prnt.sc/Q4HLW0-oY1yq

Thank you in advance, any help would be greatly appreciated.

HS.

0

1 Answer 1

3

When you're traversing throughout your data list, you need to cast each Object to Map<String, Object>. In this Map you can already retrieve id value. Then you should retrieve attributes map, from which you'll be able to get name value:

List<Object> data = (List<Object>) results.get('data');
for (Object dataIte : data) {
    Map<String, Object> dataMapIte = (Map<String, Object>) dataIte;
    String id = (String) dataMapIte.get('id');
    Map<String, Object> attributes = (Map<String, Object>) dataMapIte.get('attributes');
    String name = (String) attributes.get('name');
    System.debug(id + ' ' + name);
}

P.S.: this answer suits you, if you don't want to generate Apex class for your JSON response structure (as suggested in the comments to your question).

2
  • Although the first was somewhat helpful because I got my ids, it wasn't able to get my names of the available sites and I'm not really comfortable using json2apex because I don't have much of an experience in programming with apex. Your answer on the other hand was rock solid and you provided your thought process which for me, a newbie, is much needed. Thank you for that.
    – Stojchesky
    Aug 15 at 12:39
  • @Stojcheski You are welcome) Aug 15 at 13:36

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