Display Json Data from Scriptrunner REST Endpoint in HTML

Hi all,

This is a repost of a question I asked already in the Atlassian Community: Solved: Display Json Data from Scriptrunner REST Endpoint ...

I’m working with a Scriptrunner REST Endpoint script that takes record number values from a custom field in jira called Related CMRs, finds the matching records externally from the API of our ServiceNow instance, and displays the CMR query results as json data in a custom multi-text field called CMRs.

Here is the REST Endpoint code as follows:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
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 ServiceNow 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();
}

}

And for a test ticket in our sandbox instance, the resulting data that pulls into the CMRs field is:

{“result”:[{“u_change_record.number”:“CMR1xxxxxxx”,“u_change_record.state”:“Draft”,“u_change_record.short_description”:“test app req 5”},
{“u_change_record.number”:“CMR2xxxxxxx”,“u_change_record.state”:“Draft”,“u_change_record.short_description”:“test”},
{“u_change_record.number”:“CMR3xxxxxxx”,“u_change_record.state”:“Draft”,“u_change_record.short_description”:“Test Jira”},
{“u_change_record.number”:“CMR4xxxxxxx”,“u_change_record.state”:“Draft”,“u_change_record.short_description”:“tesst”}]}

The data output is working as expected, but I want to display the above resulting json data in a way that’s user friendly. I was wondering if I could display the above data as an html table within that multi-text CMR field to look something like this:

Would I take the values pulled directly from the api (cmrState, cmrNumber, cmrDesc) write html and assign that to the return (rt) value? Is this possible at all?

Many thanks for any tips/suggestions!

-Ian Balas