Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Format JSON data from custom REST Endpoint Edited


Hi all,

I'm tasked with formatting the returned JSON Data that's returned from this custom REST Endpoint code that we're working with:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import groovy.xml.MarkupBuilder




@BaseScript CustomEndpointDelegate delegate2

CMRDisplay(httpMethod: "GET") { MultivaluedMap queryParams ->

def query = queryParams.getFirst("query") as String

def rt = [:]
if (query) {

String url = "https://test.service-now.com"
String uriPath = "/api/now/table/u_jira_change_data"

HTTPBuilder http = new HTTPBuilder(url)
def output = http.request(Method.GET, ContentType.JSON) {
uri.path = uriPath
uri.query = [sysparm_query:"u_jira_ticket_number=$query", sysparm_fields:"u_change_record.number,u_change_record.short_description,u_change_record.state", sysparm_display_value: "true"]
headers.'Authorization' = "Basic ${"svc-jira:buO\$qguQUgat5lNVF7GH\$3VMtjaR1o".bytes.encodeBase64().toString()}"

response.failure = { resp, reader ->
log.warn("Failed to query SNow API: " + reader.text)
}
}


def cmrState = output["result"]*."u_change_record.state"
def cmrNumber = output["result"]*."u_change_record.number"
def cmrDesc = output["result"]*."u_change_record.short_description"

rt = output

return Response.ok( new JsonBuilder(rt).toString()).build();
}
}

 

For a certain issue in our instance, the output is converted to string and parsed to a multi-text field like so:

{"result":[{"u_change_record.number":"CHG0010042","u_change_record.state":"Draft","u_change_record.short_description":"test app req 5"},
{"u_change_record.number":"CHG0010061","u_change_record.state":"Draft","u_change_record.short_description":"test"},
{"u_change_record.number":"CHG0016010","u_change_record.state":"Draft","u_change_record.short_description":"Test Jira"},
{"u_change_record.number":"CHG0010057","u_change_record.state":"Draft","u_change_record.short_description":"tesst"}]}

Is there any way that I can format this output to resemble proper JSON structure?

Like below:

{
"result": [
{
"u_change_record.number":"CHG0010042",
"u_change_record.state":"Draft",
"u_change_record.short_description":"test app req 5"
},
{
"u_change_record.number":"CHG0010061",
"u_change_record.state":"Draft",
"u_change_record.short_description":"test"
},
{
"u_change_record.number":"CHG0016010",
"u_change_record.state":"Draft",
"u_change_record.short_description":"Test Jira"
},
{
"u_change_record.number":"CHG0010057",
"u_change_record.state":"Draft",
"u_change_record.short_description":"tesst"
}
]
}

 

 

1 answer

1 accepted

0 votes
Answer accepted

The first JSON output is perfectly valid for machine parsing

But if you want a pretty layout, you can output toPrettyString()

return Response.ok( new JsonBuilder(rt).toPrettyString()).build();  

Ah, that was simple enough.

Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

7,022 views 8 28
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you