How to create and update custom fields option from a json file with ScriptRunner

David Coudron January 13, 2021

Hello, 

one of our external teams feeds weekly a file with values ​​that we want to "synchronize" in a single select liste custom field.

This field is  is deposited on our server in {server}/home/deposit. 

The first step is to create the field with all the options. I know I can use the built-in script but if I can do that with console it's better for me.

The second step is to update weekly this field with the json file...
Does anyone have a sample script that would allow me to create and update this list from the file ?

Or can you just give me an example to bulk add options from a list that I can create directly in my script (to have a baseline) ? 

last question, how can I "open" my jsonfile that is on my server on a scriptrunner console ?

Best regards,

David

 

1 answer

0 votes
Ravi Sagar _Sparxsys_
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.
January 13, 2021

Hi @David Coudron 

Let me try to help.

Reading a file

You can read a file like this. The location should be accessible on the server where Jira is installed by the user running Jira.

File file = new File("/opt/jira/home/scripts/file.json")

Parsing JSON

Try this.

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(file)
//parsedJson will let you access your data
//parsedJson.id or parsedJson.name

To set custom field options

There are few possibilities. I would suggest take a look at this page and our script library where we have plenty of examples.

I hope it helps.

Ravi

David Coudron January 19, 2021

Hello Ravi,

Thanks for your answer !

I try to reading the file with your method and it's work.
But when I try to parse my json file I have the following error :

2021-01-19 19:09:32,105 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.io.File) values: [/home/user/depot/file.json] Possible solutions: parseText(java.lang.String), parse(java.io.File), parse([B), parse([C), parse(java.io.InputStream), parse(java.io.Reader) at Script21.run(Script21.groovy:8)

 

Here my code : 

import groovy.json.JsonSlurper

File file = new File("/home/adminsii/depot/file.json")

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(file)

 

Do you know what is wrong ?

Best regards,

David

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events