Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

How by jql to get issues in the form of JSON from JiraServer using java?

Hi all!

I am developing a plugin and I have the next problem:

- I retrieving from a user a JQL string like this: "project = DEMO"

- then I want to retrieve JSON with issues, but I can retrieve only List<Issue>


String query = "project = DEMO";
Query conditionQuery = jqlQueryParser.parseQuery(query);
ApplicationUser searcher = jiraAuthenticationContext.getUser();
SearchResults results =, conditionQuery, PagerFilter.getUnlimitedFilter());
List<Issue> issues = results.getIssues();


Object SearchResult is similar to JSON, retrieving from the link:


This JSON is what I need. But I can’t understand how I can get it. Maybe somebody can give me advice - how to convert SearchResult to JSON, or maybe there is another way to get the JSON I need?

2 answers

A wild banana appears!

Hi @Oleksii Skachkov 

First note that the structure behind a server / data center is not the same that the one used in cloud, so probably the best approach is to work with Java classes provided on Server / Data Center apps, and develop a different application using rest services on cloud.


Saying that, if you need to use the issue JSON representation provided by Jira REST API itself, why don't simply make an http request to the endpoint and grab the data you need?


You can use the TrustedRequestFactory provided on SAL API (Shared Layer Access) to make http request using current user authorization token, in that way you don't need to know the user password at all.

Here is a quick example I did some time ago using groovy to call the Zephyr REST API within my Jira instance


* @autor avillalobos
* To be use as a Script Listener for ProjectCreatedEvent
* Creates some test cycles by default when the project is created on behalf of a user who created the project
* Dependencies:
* - SAL-API >= 3.0
* - Zephyr add-on
* - ZAPI add-on

package com.atsistemas.groovy

import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import groovy.json.JsonOutput

// Zephyr test cycles creation
].each{ cycleName ->
createCycle(cycleName, getProject().id)

* Returns the base URL of this Jira instance
String getBaseUrl()
return ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)

* Returns the target project where test cycles will be created
* Formely, returns the project which generated this event
Project getProject()
return event.project

* Returns the user who will create the test cycles
* Formely, returns the user which generated this event
ApplicationUser getAuthor()
return event.user

* Returns the minimal payload needed to create a new Zehyr Test Cycle.
* See:
String getCyclePayload(String cycleName, Long projectId)
return JsonOutput.toJson([
name: cycleName,
projectId: projectId,
versionId: "-1"

* Create a Zephyr Test Cycle with the given name.
void createCycle(String cicleName, Long projectId)
//Get the base URL of our instance
def baseUrl = getBaseUrl()
def host = new URIBuilder(baseUrl).host

//Create the request, including the method type and REST Endpoint we would like to call
def requestFactory = ScriptRunnerImpl.getOsgiService(TrustedRequestFactory)
def request = requestFactory.createTrustedRequest(MethodType.POST, "${baseUrl}/rest/zapi/latest/cycle")

//Add the username of the user we would like to authenticate with
request.addTrustedTokenAuthentication(host, getAuthor().username)

//Add the data to the request and specify the type of data being passed
request.setRequestBody(getCyclePayload(cicleName, projectId))
request.setHeader("Content-Type", ContentType.JSON.toString())

try {
def response = request.execute() "New Zephyr cycle created '${cicleName}'...."
log.debug response

} catch(e){
log.error "Failed to created '${cicleName}'...."
log.error e.message

Hope this help.

Ignacio Pulgar Community Leader May 20, 2020

@Jack Nolddor _Sweet Bananas_Thanks for coming to my rescue! ;D

You're welcome :)

Hi, @Jack Nolddor _Sweet Bananas_ 
Thanks for your help.

I am not familiar with groovy, but the general meaning is clear. Before trying to approach from my first question, I started from http reqest. And the general problem was how to get and use user authorization token. My working code:


private HttpResponse<JsonNode> getResponce(String url) throws UnirestException {
return Unirest.get(url)
.basicAuth("admin", "admin") //TODO change auth method
.header("Accept", "application/json")


I cannot use it that way, and I need to change the basic authorization to use an authorization token. No matter which library to use. Can you give an example?

I tried using TrustedRequestFactory from your example, but this factory is no longer available.

Also, I found that Jira Server cant generate REST API token, it can use only Basic and OAuth:

Can you direct me - on which side to look for a solution to Java?

Like Ignacio Pulgar likes this

There is no way to use externals httpClients to call Jira itself if you want to avoid to hardcode user credentials. My code it a bit outdated since it worked on Jira 7, probably on Jira 8 you must use ApplicationLink#createAuthenticatedRequestFactory() or ApplicationLink#createImpersonatingAuthenticatedRequestFactory() methods.

Like Ignacio Pulgar likes this

Hi, @Jack Nolddor _Sweet Bananas_ 
Thanks for your help.

I tried to use AuthenticatedRequestFactory and AuthenticatedRequestFactory, but I did not succeed. And I decided to try on the other side. I founded how to convert JSON to SearchResult. I hope that there is a functionality that does the reverse work.

I didn't find it yet, but maybe you know about it?


No idea to be honest, I've never used the json format I've always used Java beans such us MutableIssue, Project, ...

0 votes
Ignacio Pulgar Community Leader May 19, 2020

Hi @Oleksii Skachkov ,

I'm not sure if this could be of any help:

By the way, may you elaborate on why do you need to generate a JSON? I mean, is that the goal? Or is it just a way to access issue data in a way you are more comfortable with?


Hi, @Ignacio Pulgar.
Thanks for your help.

The solution you proposed does not work. The Issue consists of other objects that also refer to the Issue. Therefore, when converting to JSON, we get recursion and Stackoverflow.

Now I'm trying to solve the issue through a chain of objects: BeanBuilderFactory -> IssueBeanFactory -> IssueBean -> SearchResultsBean to make JSON from SearchResultsBean. But I am stuck on injection BeanBuilderFactory and IssueBeanFactory. Maybe you can help with it?

My plugin should work with the Server and with the Cloud. I want to write logic only once. It would be great to get the data for calculations in the same form both from the Server and from Cloud. In addition, it will be more convenient for me to process it with a JSON.


Like Ignacio Pulgar likes this
Ignacio Pulgar Community Leader May 20, 2020

Interesting goal! This seems to be a question where @Jack Nolddor _Sweet Bananas_ (the best developer of Atlassian apps I know) may help! :)

Like Oleksii Skachkov likes this

Hey! What an incredible summoning!

May I join the party? 🎉💙

Like Ignacio Pulgar likes this
Ignacio Pulgar Community Leader May 20, 2020

Of course! You are always welcome! :)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

211 views 1 6
Read article

Atlassian Community Events