[Sharing is learning] [Get rewards] Join Us Now to Become Katalon Champions. Learn more
Binisha Babu
Katalon Apprentice
07/09/2018

How to get the JSON response list values

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?




Upvote
Quote

Comments

  • purbo
    Katalon Apprentice
    07/09/2018
    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"

    Upvote
    Quote
  • Binisha Babu
    Katalon Apprentice
    07/11/2018
    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]





    Upvote
    Quote
  • Marek Melocik
    Katalon Expert
    07/11/2018
    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")
    }

    Upvote
    Quote
Sign In or Register to comment.