How to parse issuetype REST call return value?

While developing an Atlassian Connect Cloud plugin I try to parse the return value of rest/api/2/issuetype into JSON objects to take advantage of the JRJC IssueTypeJsonParser.

What I tried so far:

ResponseEntity<String> response1 = template.getForEntity(baseUrl + JIRA_REST_BASE_PATH + "issuetype",
String.class);
LOG.info("Issuetypes response 1 is: {}", response1.getBody());
//
ResponseEntity<Object[]> response2 = template.getForEntity(baseUrl + JIRA_REST_BASE_PATH + "issuetype",
Object[].class);
Object[] issueTypes2 = response2.getBody();
LOG.info("Issuetype response 2: size = {}, objects = {}", issueTypes2.length, issueTypes2);
//
ResponseEntity<JSONObject[]> response3 = template.getForEntity(baseUrl + JIRA_REST_BASE_PATH + "issuetype",
JSONObject[].class);
JSONObject[] issueTypes3 = response3.getBody();
LOG.info("Issuetype response 3: size = {}, objects = {}", issueTypes3.length, issueTypes3);
//
ResponseEntity<JSONArray> response4 = template.getForEntity(baseUrl + JIRA_REST_BASE_PATH + "issuetype",
JSONArray.class);
JSONArray issueTypes4 = response4.getBody();
LOG.info("Issuetype response 4: size = {}, objects = {}", issueTypes4.length(), issueTypes4);

The results are:

Issuetypes response 1 is: [{"self":"https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10001","id":"10001","description":"An improvement or enhancement to an existing feature or task.","iconUrl":"https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10310&avatarType=issuetype","name":"Improvement","subtask":false,"avatarId":10310},{"self":"https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10004","id":"10004","description":"A new feature of the product, which has yet to be developed.","iconUrl":"https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10311&avatarType=issuetype","name":"New Feature","subtask":false,"avatarId":10311},{"self":"https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10005","id":"10005","description":"A problem which impairs or prevents the functions of the product.","iconUrl":"https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype","name":"Bug","subtask":false,"avatarId":10303},{"self":"https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10002","id":"10002","description":"A task that needs to be done.","iconUrl":"https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype","name":"Task","subtask":false,"avatarId":10318},{"self":"https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10000","id":"10000","description":"A big user story that needs to be broken down. Created by JIRA Software - do not edit or delete.","iconUrl":"https://kuespert-dev.atlassian.net/images/icons/issuetypes/epic.svg","name":"Epic","subtask":false},{"self":"https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10003","id":"10003","description":"The sub-task of the issue","iconUrl":"https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype","name":"Sub-task","subtask":true,"avatarId":10316}]
Issuetype response 2: size = 6, objects = [{self=https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10001, id=10001, description=An improvement or enhancement to an existing feature or task., iconUrl=https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10310&avatarType=issuetype, name=Improvement, subtask=false, avatarId=10310}, {self=https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10004, id=10004, description=A new feature of the product, which has yet to be developed., iconUrl=https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10311&avatarType=issuetype, name=New Feature, subtask=false, avatarId=10311}, {self=https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10005, id=10005, description=A problem which impairs or prevents the functions of the product., iconUrl=https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype, name=Bug, subtask=false, avatarId=10303}, {self=https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10002, id=10002, description=A task that needs to be done., iconUrl=https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype, name=Task, subtask=false, avatarId=10318}, {self=https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10000, id=10000, description=A big user story that needs to be broken down. Created by JIRA Software - do not edit or delete., iconUrl=https://kuespert-dev.atlassian.net/images/icons/issuetypes/epic.svg, name=Epic, subtask=false}, {self=https://kuespert-dev.atlassian.net/rest/api/2/issuetype/10003, id=10003, description=The sub-task of the issue, iconUrl=https://kuespert-dev.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype, name=Sub-task, subtask=true, avatarId=10316}]
Issuetype response 3: size = 6, objects = [{}, {}, {}, {}, {}, {}]

and for 4th try:

Caught org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of org.codehaus.jettison.json.JSONArray out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.codehaus.jettison.json.JSONArray out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@5aaae78e; line: 1, column: 1]

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of org.codehaus.jettison.json.JSONArray out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.codehaus.jettison.json.JSONArray out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@5aaae78e; line: 1, column: 1]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:244) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:229) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:96) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:917) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:901) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:312) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]

None of the returned values can be used to invoke

IssueTypeJsonParser parser = new IssueTypeJsonParser();
IssueType issueType = parser.parse(someObject);

How can I parse the result from the

<JIRA-BASE-URL>/rest/api/2/issuetype

REST call to get some object to work with?

What's puzzeling me is that the String response (1) contains quotes while the Object[] response doesn't.

Also the JSONObject[] response returns the correct number of entries but they are empty.

And the 4th try - which IMHO is the most elegant - throws an exception.

What's going wrong?

1 answer

1 accepted

This is how it works for me now:

ResponseEntity<String> response = template.getForEntity(baseUrl + JIRA_REST_BASE_PATH + "issuetype",
String.class);
Collection<IssueType> result = JsonParseUtil.parseJsonArray(new JSONArray(response.getBody()), new IssueTypeJsonParser());
for (IssueType type : result) {
LOG.info("Issuetype name = " + type.getName());
}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 6 hours ago in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

85 views 4 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you