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

Convert JSON to Groovy

Ramondrio Imanuel April 7, 2021

Hi,

I want to convert JSON Header into Groovy Script. I already try to write this script in my scriptrunner console :

import groovy.json.JsonSlurper 
class Example { static void main(String[] args) { 
def jsonSlurper = new JsonSlurper() 
def object = jsonSlurper.parseText('{ "name": "John", "ID" : "1"}') 
println(object.name); 
println(object.ID); } 
}

 but the result is "class Example" not name and ID.

image.png

Is there any workaround for this? Thanks. 

1 answer

0 votes
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 7, 2021

Hi @Ramondrio Imanuel ,

try to add "as Map" like this:

import groovy.json.JsonSlurper

String requestBody = '{ "name": "John", "ID" : "1"}'

JsonSlurper jsonSlurper = new JsonSlurper()
Map object = jsonSlurper.parseText(requestBody) as Map
object.name

 

Ramondrio Imanuel April 8, 2021

Hi @Hana Kučerová ,

Thanks. it works. I also want to define json headers parameters in groovy script. Is there any workaround for that? Thanks.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 8, 2021

Hi @Ramondrio Imanuel ,

I apologize, but I don't understand. Would you please provide me more information, what you are trying to achieve?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events