Parse json response


#1

Below is the code i am trying to parse a json response
def responseText = response.getResponseText()

println(responseText )
Result:

{“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”}

]

}
I tried following to retrieve the value of applications->Asset_1->id

def jsonSlurper = new JsonSlurper()

def object = jsonSlurper.parseText(responseText)
println(object.id) // Object don’t have property id so it dint worked

Possibly a way to do it could be something like this

def object = jsonSlurper.parseText(’{“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(object.applications[1].id) // But this also doesn’t works

How do I extract any value, from a key-value pair of a json response. Is there any way i can use responseText , instead of expanding entire response of json as i did above(though its also not working :slight_smile: )


#2

Hello,

this is what you want.

println object.get("applications").get(0).get("id")

applications is JSON array, so get N-th array member and get id then.


#3

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?


How to print the response of the REST API call
#4

hello,
i was trying your example and after small changes it works:

import groovy.json.JsonSlurperdef json = new JsonSlurper().parseText('{"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(json.applications[1].id)

console:

08-14-2018 09:12:47 AM - [START]  - Start Test Case : Test Cases/__Sandbox/New Test Case 708-14-2018 09:12:47 AM - [INFO]   - Evaluating variables for test case08-14-2018 09:12:48 AM - [START]  - Start action : Statement - json = new groovy.json.JsonSlurper().parseText("{"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"}]}")08-14-2018 09:12:48 AM - [END]    - End action : Statement - json = new groovy.json.JsonSlurper().parseText("{"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"}]}")08-14-2018 09:12:48 AM - [START]  - Start action : Statement - println(id)a9e0bce08-14-2018 09:12:48 AM - [END]    - End action : Statement - println(id)08-14-2018 09:12:48 AM - [PASSED] - Test Cases/__Sandbox/New Test Case 708-14-2018 09:12:48 AM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case 7


#5

Thank you Marek Melocik, kazurayam, and Andrej Podhajský for the help.