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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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

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

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

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
Community showcase
Published in Marketplace Apps & Integrations

How to log work and track time in Jira when a person is part of multiple teams

  The manager’s daily activities include a list of challenges to reach high levels of efficiency for their teams. Part of these challenges is related to how to deal with the worklog systems sin...

117 views 2 2
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