1

I have a json response as shown below. I am trying to get fileUrl field but unfortunately getting null value.

{
   "errors":false,
   "results":[
      {
         "result":{
            "fileUrl":"/services/data/v42.0/connect/files/12041skfjsdkfs/content?versionNumber=1&_bearer=kjdflkjsdfkljsdklfjsd.asljdklasjdklasfklsnkfslkflkdsf",
            "externalDocumentUrl":null  
            }           
      },
      {
         "result":{
            "fileUrl":"/services/data/v42.0/connect/files/12041skfjsdkfs/content?versionNumber=1&_bearer=kjdflkjsdfkljsdklfjsd.asljdklasjdklasfklsnkfslkflkdsf",
            "externalDocumentUrl":null  
            }           
      }
   ]
}

This is the code i have tried so far.

   map<String, Object> jsonResp =(Map<String, Object>)JSON.deserializeUntyped(response.getBody());
        List<Object> results = (List<Object>)jsonResp.get('results');
        List<Object> result = new List<Object>();
        for(Object obj: results) {
            result.add(obj);
        } 
        Map<String, Object> leftMap =  ( Map<String, Object>) result[0];
        System.debug('LFileUrl=='+leftMap.get('fileUrl'));
        Map<String, Object> rightMap =  ( Map<String, Object>) result[1];
        System.debug('RFileUrl=='+rightMap.get('fileUrl'));

In system.debug, I am getting null value.

So, really cannot get fileUrl value. What have i missed?

2

EDIT - needed an extra level just as David said.

Pretty sure you just want something like this:

String resp = response.getBody();

Map<String, Object> jsonResp =(Map<String, Object>)JSON.deserializeUntyped(resp);
List<Object> results = (List<Object>)jsonResp.get('results');

for (Object result : results){
  Map<String, Object> resultMap =  ( Map<String, Object>) result;
  Map<String, Object> resultObj =(Map<String, Object>)resultMap.get('result');
  System.debug('fileUrl=='+resultObj.get('fileUrl'));
}

Also have you tried parsing it into a nice typed object using JSON2Apex? Check it out here:

EDIT To test this code (which I have verified works), replace the http response with this static string:

resp = '{"errors":false,"results":[{"result":{"fileUrl":"/services/data/v42.0/connect/files/12041skfjsdkfs/content?versionNumber=1&_bearer=kjdflkjsdfkljsdklfjsd.asljdklasjdklasfklsnkfslkflkdsf","externalDocumentUrl":null}},{"result":{"fileUrl":"/services/data/v42.0/connect/files/12041skfjsdkfs/content?versionNumber=1&_bearer=kjdflkjsdfkljsdklfjsd.asljdklasjdklasfklsnkfslkflkdsf","externalDocumentUrl":null}}]}';
2
  • Isn't list to map conversion allowed. As, I am getting the following error using this code,-- Invalid conversion from runtime type List<ANY> to Map<String,ANY>
    – MisterA
    May 2 '18 at 3:41
  • I know the above works - try using this exact code with the static httpresponse and see what you get. May 2 '18 at 4:01
1

Your code misses a level of structure in the JSON. result contains a list of maps, each of which has exactly one key - 'result'. You need to access this intermediate object to get the 'fileURL' key.

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.