Help on groovy script to read Json script

Omprakash Thamsetty
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.
October 19, 2022

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

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 23, 2022

Hi @Omprakash Thamsetty

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:-

image1.png

I hope this helps to answer your question. :-) 

 

Thank you and Kind regards,

Ram

Omprakash Thamsetty
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.
October 23, 2022

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 24, 2022

Hi @Omprakash Thamsetty

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:-

image2.png

 

Below is a screenshot of the test issue:-

image3.png

I hope this helps to solve your question. :-)

 

Thank you and Kind regards,

Ram

Omprakash Thamsetty
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.
October 24, 2022

It worked and also changed little bit

 

def custfld = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("CustomerAdv").first()
        def custfldVal = issue.getCustomFieldValue(custfld) as String
        //def obj = jsonSlurper.parseText '''{"rows":[["53767","154"]], "ts":1666194503983, "rowNo":1}'''
        def obj = jsonSlurper.parseText(custfldVal)
        //def objrows = obj.rows as ArrayList
        //def objrowsFirst = objrows.first() as ArrayList
        //def requiredItem = objrowsFirst.elementData(1)
        //in my replication environment:
        log.warn(obj.rows.first().elementData(1))
0 votes
Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 20, 2022

Hi,

What is the type of this field ? It is provided by a plugin ?

Omprakash Thamsetty
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.
October 21, 2022
Morgan DUBUISSON
Contributor
October 21, 2022

Hi,

Maybe with this link : http://groovy-lang.org/json.html

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.8
TAGS
AUG Leaders

Atlassian Community Events