Forums

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

Create REST endpoint with Script Runner to parse input JSON

Chow King Tak February 13, 2018

Hi,

I have created a REST endpoint with POST method. The client will call the endpoint with input JSON. I still can't figure out how to parse and construct a generic JSON object from the request body in order to retrieve the input values.

 

Please enlighten and thanks.

1 answer

0 votes
Alexey Matveev
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 13, 2018

In what applications are you trying to construct and parse a generic JSON? 

Chow King Tak February 13, 2018

I tried the sample from https://scriptrunner.adaptavist.com/latest/jira/rest-endpoints.html to create a REST endpoint:

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls
import com.atlassian.jira.issue.fields.rest.json.beans.PriorityJsonBean
import com.atlassian.jira.issue.priority.Priority
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import org.codehaus.jackson.map.ObjectMapper

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

def priorityManager = ComponentAccessor.getComponent(PriorityManager)
def baseUrls = ComponentAccessor.getComponent(JiraBaseUrls)
ComponentManager.instance

priority(
httpMethod: "POST", groups: ["jira-administrators"]
) {MultivaluedMap queryParams, String body ->

def mapper = new ObjectMapper()
def bean = mapper.readValue(body, PriorityJsonBean)
assert bean.name // must provide priority name
assert bean.description // must provide priority description
assert bean.iconUrl // must provide priority icon url
assert bean.statusColor // must provide priority statusColor

Priority priority
try {
priority = priorityManager.createPriority(bean.name, bean.description, bean.iconUrl, bean.statusColor)
} catch (e) {
return Response.serverError().entity([error: e.message]).build()
}

return Response.created(new URI("/rest/api/2/priority/${priority.id}")).build()
}

But it only creates the specific Priority JSON object, so I would like to know is there any class API I could use to create a general JSON object as the client will pass a general JSON to this REST endpoint like:

{"product_name":"xxxxx", product_id:1234 }

 

Thanks.

Chow King Tak February 13, 2018

My problem is solved using groovy.json.JsonSlurper class.

Thanks.

Like # people like this
Николай Киселев July 31, 2020

Hello,

Could you tell me how you did it? I created rest endpoint but I do not know how to catch incoming json

 

Thank you

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events