Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Format JSON data from custom REST Endpoint

-
Contributor
February 15, 2021


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
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2021

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();  
-
Contributor
February 19, 2021

Ah, that was simple enough.

Thanks!

Suggest an answer

Log in or Sign up to answer