Parse json response

I tried the following test case and it worked.

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def text = '''
{"applications":[
{"name":"test123","id":"c1257c5","description":"test","type":"generic","version":"0.1"},
{"name":"Asset_1","id":"a9e0bce","description":"sfsdgdg","type":"generic","version":"0.1"},
{"name":"Asset_2","id":"a9e0cd2","description":"sffgdgf","type":"generic","version":"0.1"}
]
}
'''
println JsonOutput.prettyPrint(text)
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(text)
assert object.applications[0].id == 'c1257c5'
assert object.applications[1].id == 'a9e0bce'
assert object.applications[2].id == 'a9e0cd2'

My code seems to have no significant difference from your one. I can’t find why you got

println(object.applications[1].id) // But this also doesn’t works

Did you miss any import statements?

2 Likes