mardi 9 octobre 2018

Retrieving values from a nested response in Postman API

I am new to Postman and programming/scripting in general and need some help on retrieving values from a nested array and also verify the number of attributes returned in the response. I have the following JSON:

{
    "studentsData":[
        {
            "id": 142,
            "firstName": "Kai",
            "lastName": "Carvalho Rodrigues",
            "addressLine1": "4188 Viking Drive",
            "addressLine2": "",
            "state": "VA",
            "city": "Fairview",
            "postalCode": "43736S",
            "country": "US",
            "emergencyContacts":[
                {
                    "firstName": "Ruby",
                    "lastName": "Wilfred",
                    "phoneNumber": "5541-766-XXXX"
                },
                {
                    "firstName": "Isaac",
                    "lastName": "Jeanneret",
                    "phoneNumber": "5541-761-XXXX"                   
                }         
            ]
        },
        {
            "id": 143,
            "firstName": "Kai",
            "lastName": "Yewen",
            "addressLine1": "4956 Alexander Avenue",
            "addressLine2": "",
            "state": "CO",
            "city": "Denver",
            "postalCode": "94607",
            "country": "US",
            "emergencyContacts":[
                {
                    "firstName": "Matilda",
                    "lastName": "McAdam",
                    "phoneNumber": "988-445-XXXX"
                }
            ]
        }            
    ]
}

Here I am trying to : Print firstName under studentData Verify the firstName count in Response Print the list of phoneNumber under emergency contacts Verify the count of phoneNumber in Response Print the firstName along with phoneNumber under emergencyContacts in Postman Console.

//Printing all firstNames in Response

var jsonData = JSON.parse(responseBody);
var name = jsonData.studentsData;
for (var i = 0; i <= name.length; i++ ) {
console.log(" The list of students' first names are: "+name);

//Validating the count of firstNames in Response

var jsonData = JSON.parse(responseBody);
tests["Total number of students = 2"] = jsonData.studentsData.length === 2;

//Printing the list of phoneNumber under emergency contacts

var responseData = JSON.parse(responseBody);
var i;
var j;
var contactNumbers = jsonData.studentsData[i].emergencyContacts[j];

for (var i = 0; i <= contactNumbers.length; i++ )
    for (var j = 0; j <= contactNumbers.length; j++) {
        console.log(" The list of emergency contacts are: "+contactNumbers);
    }
}

//Validating the count of emergencyContacts in Response

    var jsonData = JSON.parse(responseBody);
    var i;
    var contactNumbers = jsonData.studentsData[i].emergencyContacts[j];
    for (i=0; i <= contactNumbers.length)
       for (var j = 0; j <= contactNumbers.length; j++){
    tests["Total number of emergency contacts = 4"] = jsonData.studentsData[I].emergencyContacts[j].length === 4;
    };

Print the firstName along with phoneNumber under emergencyContacts in Postman Console - Not able to go about this scenario

Aucun commentaire:

Enregistrer un commentaire