Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

having issues accessing the data in a returned json payload when configuring a rest endpoint

Hi all,

I am configuring a rest endpoint to populate a custom field with the results of an external API, as explained in the documentation for "Select List Conversions" in scriptrunner (https://scriptrunner.adaptavist.com/latest/jira/behaviours-conversions.html)

The issue I am having is that when I test the API in a browser, as explained in the document, I am getting an empty value:

{"items":[]}

 

That is most likely because I might be missing something in the loop where the actual values are gathered:

rt = [
items : persons["response.persons"].collect { Map person ->
def personfirstName = person.core?."firstName"
[
value: personfirstName,

]
},

Here is the extract of the jason payload:

"response": {
    "noOfRecords": 10,
    "apiVersion": "3.0",
    "params":

          { "omitNullFields": "true", "limit": "10", "q": "pappas", "offset": "1" }

,
    "persons": [
     {
        "lastUpdateDate": "2019-08-29T09:44:30Z",
         "core": {
                     "gocOfficeCode": "FIR",
                     "gocOfficeClusterCode": "FI",
                     "gocDepartmentId": 58034,
                     "jobRoleSubCategoryName": "n/a",
                     "jobRoleSubCategoryId": 1393714,
                     "firstName": "Michael",

I need to loop on the "persons" array to get the value for "firstName", which is under "core". And the "persons" array is under "response".

In the example in the documentation for the github repos, "items" is an array which is not under anything else, but in my case, "persons" array is under "response" so I am not quite sure how to access that.

Thank you for your assistance

1 answer

1 accepted

0 votes
Answer accepted

I re-created your json in a map object with only the pertinent information for testing

def resp = [
noOfRecords:2,
apiVersion:"3.0",
persons:[
[core:[firstName:"name1"]],
[core:[firstName:"name2"]]
]
]

Then, I can get this correctly formatted output like this:

def query = "2" //as an example search criteria entered by the user to be bolded in the drop down
def rt = [
items: resp.persons.collect{person->
def html = ""
if (query) {
html= person.core?.firstName.replaceAll(/(?i)$query/) { "<b>${it}</b>" }
}
[value:person.core?.firstName, label:person.core?.firstName, html:html]
}
]

 When I convert rt to a Json, I get the following:

{"items":[
{"value":"name1","label":"name1","html":"name1"},
{"value":"name2","label":"name2","html":"name<b>2</b>"}
]
}

Hi @Peter-Dave Sheehan  that worked...thank you...now that I have your attention :-) I need some assistance with something related to that same API. Now I need to pass that JSON response as the query parameter for another API I need to invoke with the GET method. Using your same JSON response example above,  name1, name2, name3, etc are codes and I need to query another API to get the actual names corresponding to those codes. Something like this:

 

q: "projectId: ("name1" OR "name2" OR ....)

a colleague of mine sent me a similar code in Java but I am not sure how to convert that to groovy so scriptrunner can take it:

q: `projectId:(${assignments.map((assignment) => assignment.projectId).join(" OR ")})`

 

assignment object is coming from the previous API and I need to query another API with that assignment response.

 

Any assistance is greatly appreciated.

 

Thank you very much

Can you share some sample code around how you are calling that GET method in the other API?

But if you are just trying to make a string of names with OR as separators:

"${resp.persons.collect{it.core.firstName}.join(' OR ')}"

Hi @Peter-Dave Sheehan  thank you for your reply...I figured it out. And yes, you are in the right track. I did use the join method along some  other string methods to get the 'q' parameter for the next API and I got it working. I do have another issue though, related to this, since the behavior associated to this rest end point works ok in jira but not that really ok in JSD. I will create another question in the Atlassian Community if necessary for that issue but what is happening is:

- when typing in the field in jira, the list of values shows the values matching the 4 characters typed (the way is supposed to work).

- when typing in the field in jira service desk, the list of values shows ALL the values being returned by the rest end point, instead of the values matching the 4 characters typed.

 

Here is the behavior, which is actually a copy of the behavior in the select list conversion documentation in scriptrunner:

 

getFieldByName("Test Text Field").convertToMultiSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/getProjectNames",
query: true, // keep going back to the sever for each keystroke
minQueryLength: 4,
keyInputPeriod: 500,
formatResponse: "general",
]
])

 

Any idea how to fix that?

 

Thank you

Sorry... not much experience yet with scriptrunner and JSD. No I have no idea here.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

51 views 0 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you