I want to validate these field but I am getting error

Here is my Json Code -

{
“status”: “true”,
“data”: {
“first_name”: “Kallol”,
“middle_name”: “Sarker”,
“last_name”: “Ananda”,
“date_of_birth”: “14/09/1997”,
“TechnicalDetails”: [
{
“id”: 90154,
“language”: [
“Bangla”,
“English”
],
“yearexp”: “Two years”,
“lastused”: “Before After”,
“st_id”: “4180051”
}
],
“Address”: [
{
“Permanent_Address”: {
“House_Number”: “40/4”,
“City”: “Dhaka”,
“State”: “Dhaka”,
“Country”: “Bangladesh”,
“PhoneNumber”: [
{
“Std_Code”: “1162090”,
“Home”: “Tower Twenty Four”,
“Mobile”: “+8801746684953”
},
{
“Std_Code”: “1110338426”,
“Home”: “Tower twenty four”,
“Mobile”: “+8801913903626”
}
]
},
“Current_Address”: null,
“stId”: “4180051”
},
{
“Permanent_Address”: {
“House_Number”: “40/4”,
“City”: “Dhaka”,
“State”: “Dhaka”,
“Country”: “Bangladesh”,
“PhoneNumber”: [
{
“Std_Code”: “1162090”,
“Home”: “Tower Twenty Four”,
“Mobile”: “+8801746684953”
},
{
“Std_Code”: “1110338426”,
“Home”: “Tower twenty four”,
“Mobile”: “+8801913903626”
}
]
},
“Current_Address”: null,
“stId”: “4180051”
},
{
“Permanent_Address”: {
“House_Number”: “40/4”,
“City”: “Dhaka”,
“State”: “Dhaka”,
“Country”: “Bangladesh”,
“PhoneNumber”: [
{
“Std_Code”: “1162090”,
“Home”: “Tower Twenty Four”,
“Mobile”: “+8801746684953”
},
{
“Std_Code”: “1110338426”,
“Home”: “Tower twenty four”,
“Mobile”: “+8801913903626”
}
]
},
“Current_Address”: null,
“stId”: “4180051”
},
{
“Permanent_Address”: {
“House_Number”: “40/4”,
“City”: “Dhaka”,
“State”: “Dhaka”,
“Country”: “Bangladesh”,
“PhoneNumber”: [
{
“Std_Code”: “1162090”,
“Home”: “Tower Twenty Four”,
“Mobile”: “+8801746684953”
},
{
“Std_Code”: “1110338426”,
“Home”: “Tower twenty four”,
“Mobile”: “+8801913903626”
}
]
},
“Current_Address”: null,
“stId”: “4180051”
}
]
}
}

And I want to validate - Write a test case for (Language, Year of Experience, House Number, City, Country, Mobile, Current Address)
field value validation
Please anyone help about this problem help. Thank you.

Can you please use the preformatted text option in the editor when posting code or JSON examples. It stops the code all being aligned to the left, which makes it hard to read, and work out what is an object or an array at a glance.

The following is an example of getting the language.

jsonData = pm.response.json();

pm.test("Language field value validation", function () {
    pm.expect(jsonData.data.TechnicalDetails[0].language).to.eql(["Bangla", "English"]);
});

The Technical Details element is an array with one element in it, so you need to target the array using square brackets. Array’s start at zero. So TechnicalDetails[0]

"TechnicalDetails": [
    {
    "id": 90154,
    "language": [
        "Bangla",
        "English"
        ],
    "yearexp": "Two years",
    "lastused": "Before After",
    "st_id": "4180051"
    }
],

You have two languages which are in their own array. You can validate both at the same time by defining the assertion as an array. [“Bangla”, “English”]

As for the address\mobile number, etc. Which one do you want to assert against, as you have an array of addresses? The principle is the same, you have elements that are arrays, which need to be targeted.

1 Like

On a side note, isn’t this a course that you are doing. Doesn’t the course explain these points to you. Isn’t us giving you the answers defeating the purpose of the training that you are having.

sir, it’s a dummy project for practice purposes. I have missed this class from my course teacher.
After that, I have to try to do this project and I faced errors. Then I have posted in the postman community.

Really, sir, You are awesome. Your explanation very clear to understand problem easily.

1 Like

@kallol-sarker

Here is a way to test all of the mobile numbers. Something a little bit more advanced.

It has two forEach loops that loops through the addresses array, then the phoneNumbers array.

It’s using the array index to customise the test name, to help you find which element is failing.

The assertion itself is just checking that the phone number string starts with +88.

Change one of the mobile number to +89 and it will fail that test. (Like I did for the last number in the list).

jsonData = pm.response.json();

pm.test("Language field value validation", function () {
    pm.expect(jsonData.data.TechnicalDetails[0].language).to.eql(["Bangla", "English"]);
});

jsonData.data.Address.forEach((address, index) => { // loop through the addresses
    address.Permanent_Address.PhoneNumber.forEach((phoneNumber, index2) => { // loop through the phoneNumbers
        let mobile = phoneNumber.Mobile; // used to customise the test name
        pm.test(`Address[${index}]Mobile[${index2}] - Expect ${mobile} to start with +88 `, () => {
            pm.expect(mobile.startsWith("+88")).to.be.true;
        });
    });
});

1 Like

Thanks a lot, sir. Though it’s advanced topic for me. I am not thinking like that.