You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.
View groupJoin the community to find out what other Atlassian users are discussing, debating and creating.
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"
}
]
}
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();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.