how can i return and parse json values in script console

ali kemal cem özdemir April 24, 2018

Hey guys, i want to create a issue on remote jira with jira  rest api. Codes in below, it's working great.  But i want to return json values, because when the issue created, json returns with issue id.  I need this id.

values:

{"id":"117216","key":"TEKFENEHM-1672","self":""}

 

How can i get id. Can you help me please. Best regards.



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import groovy.json.StreamingJsonBuilder
 
def baseURL = "full_url/rest/api/2/issue/";
def authString = "username:password".getBytes().encodeBase64().toString();
 
def body_req = [
           "fields": [
            "project":
            [
                "key": "TEKFENEHM"
 ],
            "summary": "REST API TEST example",
            "issuetype": [
                "name": "Question"
 ]
 
        ]
]
URL url = new URL(baseURL);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestProperty( "Authorization", "Basic ${authString}" );
connection.requestMethod = "POST";
connection.doOutput = true;
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect();

 

1 answer

1 accepted

1 vote
Answer accepted
Deleted user April 25, 2018

Hi Ali,

 

I'm having now the same issue. I would like to use the response of my POST, because i need the id too.

 

If you find the solution inform at this question, please!

I will be looking for the solution too.

ali kemal cem özdemir April 26, 2018

Hey man, yes i found the solution.

I used the getInputStream method.

Here is the some documantations about this method.

https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getInputStream()

 

and here is the my codes.

            InputStream response = connection.getInputStream();
           
            String response_string =response.getText()

            def jsonSlurper = new JsonSlurper()
            def json_object = jsonSlurper.parseText(response_string)
            assert json_object instanceof Map  
      return json_object.id

 If you have a problem please notify me. Regards.

Like # people like this
Pradeep A June 17, 2021

HI 

 I want to create an issue from Jira and get the issue details through the API. I need to receive the issue details, So that I can work with my third party app. Hope you can Understand my scenario. Please revert back if you have any solutions.

Thanks

Suggest an answer

Log in or Sign up to answer