Forums

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

REST Endpoint retrieving nested variables

apuskarcik August 16, 2019

Hello,

I would like to ask for your help regarding REST Endpoint in ScriptRunner.

The payload from the API call looks like this:

 "content": 
"{
\"affectedVersion\":[],
\"billing\":[\"Test\"],
\"browser\":[\"Browser default value\"],
\"environment\":[\"dev\"],
\"exceptions\":[\"Default exceptions value\"],
\"attachments\":[
{
\"filename\":\"2019-08-14 21_48_05-Window.jpg\",
\"contentType\":\"image/jpeg\",
\"content\":\"\noAKKKKACi...KKKACiiigD\"
}]
}

I am able to retrieve everything through the REST Endpoint script except the attachments. I would like to retrieve the filename, contentType, and content and put it into variables as for all other parameters (affectedVersion, billing, browser...).

However, the code provided by the support (https://bitbucket.org/snippets/Adaptavist/jeGxa8) throws an error:

def forms = new JsonSlurper().parseText(body) as Map<String,String>

forms.attachments.each { String fileName = it.filename // not working String contentType = it.contentType // not working String content = it.content // not working
}

So, I am unable to pass the data from the payload to the variables and create the attachments.

Is there any other way to retrieve these 'nested' attributes from the API call?

I was looking at http://grails.asia/groovy-each-examples, but there is no example of the case I am dealing with. 

I would be very happy for any help!

Thanks!

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.
August 19, 2019

"not working" is a little vague.

At first glance, your code is what I would use to access those nested object.s But since we have no way of being sure that this is how the REST API is actually retrieving it, you may need to add a whole bunch of debug message to get to the cause.

def forms = new JsonSlurper().parseText(body) as Map<String,String>
log.debug "forms contains: ${forms.keySet()}"
log.debug "attachments is of class ${forms.attachments.getClass()}" //expect arrary
log.debug "attachments contains ${forms.attachments.size()} items"
forms.attachments.each { log.debug "each attachment is of class ${it.getClass()}" //expect map
log.debud "attachment (it) has keys: ${it.keySet()}"
String fileName = it.filename // not working String contentType = it.contentType // not working String content = it.content // not working
}

 Examining your log output with these should give you a clue. Either "attachments" is not an array and each item is not a map. Or something else is wrong and you'll get some stack trace.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events