1

I'm trying to make use of the Jira Rest API from within Salesforce. I am somewhat useless with Java, so hopefully someone can point me in the right direction.

I can use the following Apex to get an initial response from the Jira API.

public class JiraInsight {


  static public void InsightId () {
  
    HttpRequest feedRequest = new HttpRequest();
    feedRequest.setEndpoint('callout:jira/rest/servicedeskapi/insight/workspace');
    feedRequest.setMethod('GET');
    Http http = new Http();
    HTTPResponse feedResponse = http.send(feedRequest);
    while (feedResponse.getStatusCode() == 302) {
                    feedRequest.setEndpoint(feedResponse.getHeader('Location'));
                    feedResponse = new Http().send(feedRequest);
                }
    
    // debug show raw response body
    System.debug(feedResponse.getBody());

    // convert json of response to map
    String jsonStr = feedResponse.getBody();
    Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(jsonStr);
    // debug show map
    System.debug(result);
}
}

This gives me the following JSON:

{
   "size":1,
   "start":0,
   "limit":50,
   "isLastPage":true,
   "_links":{
      "self":"https://XXXXXX.atlassian.net/rest/servicedeskapi/insight/workspace",
      "base":"https://XXXXXX.atlassian.net",
      "context":""
   },
   "values":[
      {
         "workspaceId":"12345-6789-12345-6789-123456789"
      }
   ]
}

and this map...

{_links={base=https://XXXXXX.atlassian.net, context=, self=https://XXXXXX.atlassian.net/rest/servicedeskapi/insight/workspace}, isLastPage=true, limit=50, size=1, start=0, values=({workspaceId=12345-6789-12345-6789-123456789})}

I need to utilise the "workspaceId" to make any subsequent requests. However, when I map JSON to a map, the nested dictionary results in a list of strings, not a list of key value pairs that I can access by key.

How can I access the "workspaceID" (or any future nested dictionaries) within Apex.

3 Answers 3

3

When working with untyped JSON deserialization, you end up needing to do a lot of explicit typecasting.

To get at your workspaceId, you would...

// Because "values" is a JSON array, cast it to a list
List<Object> values = (List<Object>)result.get('values');

// Because each array value is a JSON object, cast it to a Map<String, Object>
Map<String, Object> valueIndex0 = (Map<String, Object>)values[0];

// Extract your target value, and cast it appropriately
String workspaceId = (String)valueIndex0.get('workspaceId');
2
  • I seemed to discover this myself as you posted. Both solutions seem to work. I imagine yours is the more correct method.
    – domb84
    May 18, 2022 at 15:38
  • @domb84 Looks like the only difference in our answers is that you included extra parenthesis. They're optional in this case, but would be required (I think) if you tried to do multiple typecasts in a single statement (so you can remove ambiguity and cast the appropriate intermediate value).
    – Derek F
    May 18, 2022 at 15:50
4

I'm the author of the libshare

JsonMapper is a utility class I wrote as part of libshare opensource package. This makes it easy to read raw json.

For ex.,

new JsonMapper(jsonStr).getString('values[0].workspaceId')

Check out some examples here

That library has lots of other utilities that we found very useful as part of our consulting work with many enterprise customers.

2
  • 1
    Thanks for sharing this lib; I love stuff like this
    – cropredy
    May 18, 2022 at 19:11
  • Looks great! Much more Python like.
    – domb84
    May 19, 2022 at 19:20
3

Actually resolved this myself after struggling with it for far too long! Problem was that the key/value list is the first item.

    // convert json of response to map
    String jsonStr = feedResponse.getBody();
    Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(jsonStr);
    // debug show map
    System.debug(result);
    
    // access information within map
    List<Object> resultList= (List<Object>) (result.get('values'));
    // debug show item 0 object list
    System.debug(resultList[0]);
  
    // Convert nested dict to map
    Map<String, Object> nestedMap = (Map<String, Object>) (resultList[0]);
    System.debug(nestedMap);
    
    
    // Get workspaceId from key value pair
    System.debug(nestedMap.get('workspaceId'));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .