How to get the JSON response list values


#1

My scenario is like, I get a list of students details from response text in json format.

“StudentList”:[
{
“studentId”:“1234”,
“name”:“abc”,
“state”:“S1247”,
“city”:“Chennai”,
“zipCode”:“686667”,
“createdBy”:“test user”
},
{
“studentId”:“5679”,
“name”:“ced”,
“state”:“S1233”,
“city”:“Bangalore”,
“zipCode”:“560075”,
“createdBy”:“test user”
},
{
“studentId”:“7899”,
“name”:“teud”,
“state”:“S1233”,
“city”:“Bangalore”,
“zipCode”:“560075”,
“createdBy”:“test user”
},

I would like to verify particular student id, name and the number of students from the response. How can I do it in katalon?


#2

do you mean you want to verify student name, id and number on list number 1 for example?

you may use JsonSlurper to parse the response.

JsonSlurper parser = new JsonSlurper()

def parsedResponse = parser.parseText(ResponseBody)

assert studentId[0]==“1234”

assert name[0]==“abc”


#3

Thanks purbo.

But its not working.

{

“responseObject”: {

“StudentList”:[
{
“studentId”:“1234”,
“name”:“abc”,
“state”:“S1247”,
“city”:“Chennai”,
“zipCode”:“686667”,
“createdBy”:“test user”
},

for the above one I was able to do as below.

def jsonObject = new JsonSlurper().parseText(response.getResponseText())

jsonObject.responseObject.StudentList.studentId[0]


#4

Hello,

for your JSON example, this should work:

String lines = '{"StudentList": [{"studentId": "1234","name": "abc","state": "S1247","city": "Chennai","zipCode": "686667","createdBy": "test user"},{"studentId": "5679","name": "ced","state": "S1233","city": "Bangalore","zipCode": "560075","createdBy": "test user"},{"studentId": "7899","name": "teud","state": "S1233","city": "Bangalore","zipCode": "560075","createdBy": "test user"}]}'Map resp = new JsonSlurper().parseText(lines)ArrayList details = resp.get("StudentList")for(Map oneStud in details) {	println oneStud.get("name")}