How to read array of nested json response in postman

Hello Community,

I am getting error while comparing Nested Json response through csv file, see belwo details of the problem.

Test:
pm.test(‘Verify OrganizationId’, function () {
let a = data[‘OrganizationId’],
b = _.get(pm.response.json(), ‘OrganizationId’);
pm.expect(a).is.equal(b);
});

Response Body:

{
“Data”: [
{
“OrganizationId”: “{871e24e7-cf96-4ecc-801a-8d973bc85c16}”,
“OrganizationName”: “SP EL PASO PUB LIB”,
“ESPLibraryId”: “1230”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2021-04-19T12:10:24.437”
},
{
“OrganizationId”: “{65632ebc-6a27-4de6-addd-203e61e6a0f0}”,
“OrganizationName”: “collectionHQ - Large Branch”,
“ESPLibraryId”: “1230”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2019-12-10T17:31:27.46”
},
{
“OrganizationId”: “{5d773371-b7a6-4153-9ab0-1ef3ca9afd5b}”,
“OrganizationName”: “WATERFORD TWP FREE PUBLIC LIBRARY”,
“ESPLibraryId”: “2936”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2019-12-10T17:31:27.46”
},
{
“OrganizationId”: “{d7c9ec0e-1843-41fe-a2eb-f7fedf62a73d}”,
“OrganizationName”: “S & S Academic Library Test”,
“ESPLibraryId”: “2935”,
“IsRankEnabled”: “N”,
“IsDistributionEnabled”: “N”,
“LastUpdatedDate”: “2021-07-15T02:44:08.78”
},
{
“OrganizationId”: “{03a21413-fd9d-4bf7-820d-30b0c4bb9410}”,
“OrganizationName”: “DES MOINES - LARGE SCHOOL - 639”,
“ESPLibraryId”: “2935”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2019-12-10T17:31:27.46”
},

Error in Console Collection Runner:
Verify OrganizationId | AssertionError: expected ‘871e24e7-cf96-4ecc-801a-8d973bc85c16’ to equal undefined

It’s finding A, but B is coming back undefined.

What is this meant to be doing?

b = _.get(pm.response.json(), ‘OrganizationId’);

The normal way of working with responses is to parse the response first.

const response = pm.response.json();

and work from there.

Get used to Console logging your variables including your response, so you can what is being returned which should also show you any undefined variables. Start with the whole response, and you can then refine the console log to target the elements one by one until you get where you need to be.

I’m assuming that you are trying to confirm that the OrganizationId from your CSV file exists somewhere in the array?

This can be done using the JavaScript find function.

Something like.

const response = pm.response.json();

let OrganizationId = "{65632ebc-6a27-4de6-addd-203e61e6a0f0}";  
// let OrganizationId = data[‘OrganizationId’]

let search = (response.Data.find(obj => {return obj.OrganizationId === OrganizationId})).OrganizationId
console.log(search);

pm.test(`OrganizationId ${OrganizationId} exists`, () => {
    pm.expect(search).to.be.a("string");
    pm.expect(search).to.eql(OrganizationId);
})

It’s worth noting that your OrganizationId in the response is actually an object in a string. I really wish developers would not wrap objects in strings. It means its harder to target those elements. It also means that your CSV file needs to contain the object elements (curly brackets) otherwise the find will not work.

Can you please use the preformatted text option in the editor when posting code or JSON. It stops everything from being aligned to the left which makes it difficult to read.

@michaelderekjones Thanks for your quick response, I am getting an error in console:
“ReferenceError: Cannot access ‘OrganizationId’ before initialization”

However, I am able to achieve the desired results using below script:
pm.test (“Verify OrganizationId Present”, function () {

pm.expect(pm.response.text()).to.include(pm.iterationData.get (“OrganizationId”));

console.log ("OrganizationId “+ data[“OrganizationId”]+ " is present”)

});

The OrganizationId error will be related to how that variable is being referenced.

My example is hardcoding it for testing purposes. I’m not sure how you have this in your code. It sounds like its being defined in the wrong place.

pm.response.text().to.include is a fairly high level test. Its just checking the whole text of the response for the string.

It’s not a very accurate test, but might be absolutely fine for your situation.

Using the JavaScript find function when working with arrays its a more targeted approach and is something you might want to practice. Getting used to targeting elements in the response and working with arrays is something you will use time and time again.

@michaelderekjones
Thanks for guiding me on correct way, It worked perfectly.

However, I came with a new problem.

Now, I want to verify Other elements withing one tag, let’s say I want to Verify {5d773371-b7a6-4153-9ab0-1ef3ca9afd5b} organization has 1230 value in ESPLibraryId tag.

Could you please have a look again.

Input .csv :

OrganizationId ESPLibraryId
{871e24e7-cf96-4ecc-801a-8d973bc85c16} 1230
{65632ebc-6a27-4de6-addd-203e61e6a0f0} 1230
{5d773371-b7a6-4153-9ab0-1ef3ca9afd5b} 1230 This should fail for ESPLibraryID

Current Test:

let OrganizationId = data[“OrganizationId”]
let ESPLibraryId = data[“ESPLibraryId”]

let response = pm.response.json();

let search = (response.Data.find(obj => {return obj.OrganizationId === OrganizationId})).OrganizationId
console.log(search);

pm.test(OrganizationId ${OrganizationId} exists, () => {
pm.expect(search).to.be.a(“string”);
pm.expect(search).to.eql(OrganizationId);
})

Response Body:
{
“Data”: [
{
“OrganizationId”: “{871e24e7-cf96-4ecc-801a-8d973bc85c16}”,
“OrganizationName”: “SP EL PASO PUB LIB”,
“ESPLibraryId”: “1230”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2021-04-19T12:10:24.437”
},
{
“OrganizationId”: “{65632ebc-6a27-4de6-addd-203e61e6a0f0}”,
“OrganizationName”: “collectionHQ - Large Branch”,
“ESPLibraryId”: “1230”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2019-12-10T17:31:27.46”
},
{
“OrganizationId”: “{5d773371-b7a6-4153-9ab0-1ef3ca9afd5b}”,
“OrganizationName”: “WATERFORD TWP FREE PUBLIC LIBRARY”,
“ESPLibraryId”: “2936”,
“IsRankEnabled”: “Y”,
“IsDistributionEnabled”: “Y”,
“LastUpdatedDate”: “2019-12-10T17:31:27.46”
},

Change the search to bring back the whole object instead of just the OrganizationId.

You can then test both elements.

const response = pm.response.json();

let OrganizationId = "{65632ebc-6a27-4de6-addd-203e61e6a0f0}";
let ESPLibraryId = "1230";

// let search = (response.Data.find(obj => {return obj.OrganizationId === OrganizationId})).OrganizationId
let search = (response.Data.find(obj => {return obj.OrganizationId === OrganizationId}));

console.log(search);

pm.test(`OrganizationId ${OrganizationId} exists`, () => {

    // pm.expect(search).to.be.a("string");
    pm.expect(search).to.be.an("object");

    pm.expect(search.OrganizationId).to.eql(OrganizationId);
    pm.expect(search.ESPLibraryId).to.eql(ESPLibraryId);

})

@michaelderekjones
Not sure what’s happening, it’s getting the exact value to be matched but still failing.

Test
const response = pm.response.json();

let OrganizationId = data[“OrganizationId”]
let ESPLibraryId = data[“ESPLibraryId”]
let LastUpdatedDate = data[“LastUpdatedDate”]

let search = (response.Data.find(obj => {return obj.OrganizationId === OrganizationId}));

console.log(search);

pm.test(OrganizationId ${OrganizationId} exists, () => {

pm.expect(search).to.be.an("object");
pm.expect(search.OrganizationId).to.eql(OrganizationId);
pm.expect(search.ESPLibraryId).to.eql(ESPLibraryId);
pm.expect(search.LastUpdatedDate).to.eql(LastUpdatedDate);

})

InputData

OrganizationId ESPLibraryId LastUpdatedDate Expected Result
{871e24e7-cf96-4ecc-801a-8d973bc85c16} 1230 2021-04-19T12:10:24.437 All 3 test cases Pass
{65632ebc-6a27-4de6-addd-203e61e6a0f0} 1230 2021-04-19T12:10:24.437 LastUpdatedDate test fail
{5d773371-b7a6-4153-9ab0-1ef3ca9afd5b} 1231 2021-04-19T12:10:24.437 ESPLibraryId and test fail
Test to check failure Test to check failure Test to check failure All three test fail

Result

That is because your ESPLibraryId in the CSV file is a number but in your response it’s a string.

You have two options.

You can JSON.parse the response element (turning it into a number)

    pm.expect(JSON.parse(search.ESPLibraryId)).to.eql(ESPLibraryId);

Or you can JSON.stringify the CSV element (turning that element into a string).

    pm.expect(search.ESPLibraryId).to.eql(JSON.stringify(ESPLibraryId));

Either method will make the test pass.

On a side note, I recommend searching for the data first then having three separate tests for the OrganizationID, ESPLibraryId and the LastUpdatedDate.

If the ESPLibraryId test fails, it will not evaluate the LastUpdatedDate assertion. You will not know if the LastUpdatedDate assertion has failed or not.

I always recommend to make your tests singular as possible, so you get feedback on all of the assertions.

It’s ok sometimes to have multiple assertions in a single test, but it can make troubleshooting harder, and you may have more than one issues and not even realise it.

Your expected results says “all 3 test cases pass”.

You currently have one test case with 3 assertions.

1 Like

@michaelderekjones Thanks for your help on this.

See below the complete working script:
const response = pm.response.json();

let OrganizationId = data[“OrganizationId”]
let ESPLibraryId = data[“ESPLibraryId”]
let LastUpdatedDate = data[“LastUpdatedDate”]

let search = (response.Data.find(obj => {return obj.OrganizationId === OrganizationId}));

console.log(search);

pm.test(OrganizationId ${OrganizationId} exists + ESPLibraryId ${ESPLibraryId} exists + LastUpdatedDate ${LastUpdatedDate} exists, () => {

pm.expect(search).to.be.an("object");
pm.expect(search.OrganizationId).to.eql(OrganizationId);
pm.expect(JSON.parse(search.ESPLibraryId)).to.eql(ESPLibraryId);
pm.expect(search.LastUpdatedDate).to.eql(LastUpdatedDate);

})