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();
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.