Hi,
I am getting one of the database custom field value in Json format
{"rows":[["53767","154"]],"ts":1666193529260,"rowNo":1}
so I need the value 154 . How I can write the groovy script to get 154 value?
We have script runner plugin installed in my jira data center instance.
Thanks,
Om
For your requirement, you could try using the ScriptRunner console.
Below is a sample script for your reference:-
import groovy.json.JsonSlurper
def input = "{\"rows\":[[\"53767\",\"154\"]],\"ts\":1666193529260,\"rowNo\":1}"
def jsonResult = new JsonSlurper().parseText(input)
def filter = jsonResult['rows'] as List
def result = filter.last() as List
log.warn "====>>> ${result.last()}"
Please note that the sample script provided is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the test using the ScriptRunner console:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
Hi Ram,
Thank you for taking time on this and helping me with this.
Your script works if we have the json hard code. But How I can assign the value to input variable in line 2 ? JSON value is coming from custom field so I am using issue.getcustomFieldValue(customfieldName)
If I use
def input = issue.getcustomFieldvalue(customfieldName) then it is not assigning the value. I need to use some quotes itseems.
Thanks,
Om
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you are trying to get the value from a custom field, you will need to modify the code accordingly to:-
import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonSlurper
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def issue = issueManager.getIssueByKeyIgnoreCase('MOCK-1')
def sampleField = customFieldManager.getCustomFieldObjectsByName('Sample Text Field').first()
def sampleFieldValue = issue.getCustomFieldValue(sampleField) as String
def jsonResult = new JsonSlurper().parseText(sampleFieldValue)
def filter = jsonResult['rows'] as List
def result = filter.last() as List
log.warn "====>>> ${result.last()}"
Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.
The only modification I have made to the code is to specify the issue, the field and the field's value, which is the JSON value. The result returned is the same.
I have replaced the variable input with the sampleFieldValue.
Below is a screenshot of the Script Console configuration with the result:-
Below is a screenshot of the test issue:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It worked and also changed little bit
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
What is the type of this field ? It is provided by a plugin ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Power database custom field
https://marketplace.atlassian.com/apps/630415/power-database-fields?hosting=datacenter&tab=overview
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.