Forums

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

How to get an Array field from a Json content using Script runner in jira

serge calderara
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 Champions.
July 6, 2020

Dear all,

I have a the following code whih return a Json response from an http request which is returned as below and is an Array of a single item ( The return data format is given from MSGraph Api ):

 

public String getGuestUserId(String AuthToken,String userEmail){

    String _userId
    def http = new HTTPBuilder(graph_base_user_url + "?")        http.request(GET) {            requestContentType = ContentType.JSON
            //uri.query = [ $filter:"mail eq '$userEmail'"].toString()            uri.query=[$filter:"mail eq '$userEmail'"]            headers.'Authorization' = "Bearer " + AuthToken    

            response.success = { resp, json ->                _userId=json["value"]
            }

            // user ID not found : error 404            response.'404' = { resp ->       
                _userId = 'Not Found'
            }

        }        _userId
    } 

The return json value is as below :

[{businessPhones=[], displayName=user test, givenName=null, jobTitle=null, 
mail=user1@gmail.com, mobilePhone=null, officeLocation=null, preferredLanguage=null, 
surname=null, userPrincipalName=user1_gmail.com#EXT#@rlxcom.onmicrosoft.com, id=7982c558- ba50-4380-9e94-114d8b340720}]

What I am trying to do from Script runner is to retrive only the Id member field

 

From the script console I have try to access it as below :

def ret =apiHelper.getGuestUserId(apiHelper.Token,email)

def json = new groovy.json.JsonSlurper().parseText ret

But is does not work and I get an exception as below:

 groovy.json.JsonException: expecting '}' or ',' but got current char 'b' with an int value of 98 The current character read is 'b' with an int value of 98 expecting '}' or ',' but got current char 'b' with an int value of 98 line number 1 index number 2 [{businessPhones=[], displayName=serge cal test, givenName=null, jobTitle=null, mail=calderara.serge@gmail.com, mobilePhone=null, officeLocation=null, preferredLanguage=null, surname=null, userPrincipalName=calderara.serge_gmail.com#EXT#@rlxcom.onmicrosoft.com, id=7982c558-ba50-4380-9e94-114d8b340720}] ..^ at org.apache.groovy.json.internal.JsonParserCharArray.complain(JsonParserCharArray.java:149) at org.apache.groovy.json.internal.JsonParserCharArray.decodeJsonObject(JsonParserCharArray.java:140) at org.apache.groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:182) at org.apache.groovy.json.internal.JsonParserCharArray.decodeJsonArray(JsonParserCharArray.java:322) at org.apache.groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:178) at org.apache.groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:153) at org.apache.groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:43) at org.apache.groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:380) at org.apache.groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:110) at Script127.run(Script127.groovy:20)

Any idea how can I return the ID part of the Json string ?

Thanks for help

regards 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Dionathan Lopes {ALM Works}
Atlassian Partner
October 28, 2021

Hello @Michael Shaffer Sr 

Dionathan from ALM Works here.

There is a known issue where the addedaftersprintstarts JQL function does not work inside structure the same way it works in an issue search.

There is a workaround that can help to make it work:

1. Access Structure's Advanced configuration, add the structure.job.emulateHttpRequest property and set it to true.
https://wiki.almworks.com/display/structure/Advanced+Configuration+and+Dark+Features
2. Reinstall Structure after that.

Please check if the function works as expected and monitor the Structure for a while to make sure that there is no strange behavior anywhere.

Please let me know how it goes.

Best regards,
Dionathan L
www.almworks.com

Michael Shaffer Sr
March 11, 2024

Thanks but I am not an admin so I cannot use the work around.

TAGS
AUG Leaders

Atlassian Community Events