0

I'm retrieving this response from a REST callout for Layouts and I want to access a current key of this Object:

This is how I managed response:

Map<String,Object> tokenResponse = (Map<String,Object>) JSON.deserializeUntyped(body);
System.debug('*****pa1: ' + tokenResponse);

body is:

{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01Gw000001F9kGrEAJ"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00hw00000

and *****pa1:

{done=true, entityTypeName=ProfileLayout, queryLocator=null, records=({Layout={Name=BI_Ciclo de ventas completo, attributes={type=Layout, url=/services/data/v41.0/tooling/sobjects/Layout/00hw000000QB6GSAA1}}, attributes={type=ProfileLayout, url=/services/data/v41.0/tooling/sobjects/ProfileLayout/01Gw000001F9kGrEAJ}}), size=1, totalSize=1}

And I want to access **records -> [0] -> Layout -> attributes -> url

How can I achieve this?

2

Explanation:

if parts are used to be sure that response contains all needed data.

For example: if tokenResponse does not contains key 'records', than it will throw null-pointer exception when retrieving it.

Same for checking the size of the 'records' returned, because records[0] may not exist.

if (tokenResponse.containsKey('records')) {
    List<Object> records = (List<Object>) tokenResponse.get('records');

    if (records.size() > 0) {
        Map<String, Object> record  = (Map<String, Object>) records[0];

        if (record.containsKey('Layout')) {
            Map<String, Object> layout = (Map<String, Object>) record.get('Layout');

            if (layout.containsKey('attributes')) {
                Map<String, Object> attributes = (Map<String, Object>) layout.get('attributes');

                if (attributes.containsKey('url')) {
                    String url = (String) attributes.get('url');
                    system.debug(url);
                }
            }
        }
    }
}

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.