Verify the first Json response Object values are present in second Json Response Object values

Hello Team ,

can someone please help me here

I have 2 Json Reponses , I need to verify that first objects value are present in Second objects values

first object name and second object name are different

in first response I have 977 objects same name below is example

{

"name": "i_vestmark_tpi_household_allocations",

"elements": [

    {

        "household_name": "Test Household DEF"

    },

    {

        "household_name": "test"

    },

    {

        "household_name": "Test Household 3"

    },

    {

        "household_name": "Test WM"

    },

    {

        "household_name": "Test HH Sandeep"

    },

    {

        "household_name": "Test Co-Relationship Household"

    },

    {

        "household_name": "Test Sandy 1"

    },

In 2 nd Response I have 4000 objects

{

"name": "i_source_data_salesforce_tpi_target_allo_v2",

"elements": [

    {

        "householdname": "Test Household DEF",

        "organization": "TPI"

    },

    {

        "householdname": "test",

        "organization": "TPI"

    },

    {

        "householdname": "Test Household 3",

        "organization": "TPI"

    },

    {

        "householdname": "Test WM",

        "organization": "TPI"

    },

    {

        "householdname": "Test HH Sandeep",

        "organization": "TPI"

    },

    {

        "householdname": "Test Co-Relationship Household",

        "organization": "TPI"

    },

    {

        "householdname": "Test Sandy 1",

        "organization": "TPI"

    },

    {

        "householdname": "Test HHs",

        "organization": "TPI"

    },

    {

        "householdname": "Lee, Jeffrey Household",

        "organization": "TPI"

    },

    {

        "householdname": "D'Alessandro, Gail Household",

        "organization": "TPI"

    },

    {

        "householdname": "Lee, Michelle Household",

        "organization": "TPI"

    },

    {

        "householdname": "Lee, Andrew Household",

        "organization": "TPI"

    },

    {

        "householdname": "ZZX-Lee, Christopher Household",

        "organization": "TPI"

    },

    {

        "householdname": "Lee, Christopher Household",

        "organization": "TPI"

    },

    {

        "householdname": "McCarthy, Kate & Bryan Household",

        "organization": "TPI"

    },

    {

        "householdname": "Richards, Rob Household",

        "organization": "TPI"

    },

    {

        "householdname": "DiOrio, Chris & Veronica Household",

        "organization": "TPI"

    },

    {

        "householdname": "ZZX-Sagalyn, Rita",

        "organization": "TPI"

    },

    {

        "householdname": "Sawhney-Martin, Shilpa & James Household",

        "organization": "TPI"

    },

    {

        "householdname": "Dvorstov, Mikhail & Elena Household",

        "organization": "TPI"

    },

    {

        "householdname": "Percy, Jane Household",

        "organization": "TPI"

    },

    {

        "householdname": "Joel, Michael Household",

        "organization": "TPI"

    },

    {

        "householdname": "ZZ-TEST",

        "organization": "TPI"

    },

Here I need to verify that household_name values from first response are present in Second householdname values .

I have tried it with for loop , can we do other way simple with assertions?

I have written below like in in my First Request Test TAB

let jsonData = pm.response.json().elements

pm.test(ā€œsuccess household_name column stored as variableā€, function () {

jsonData.forEach((item, index) => {

pm.collectionVariables.set(one-${index}, item[ā€œhousehold_nameā€])

})

});

let DataSize = pm.response.json().elements.length

pm.environment.set(ā€œSizeā€,DataSize);

I have written below like in my Second Request Test TAB
et jsonData = pm.response.json().elements

var secondValue = ;

jsonData.forEach((item, index) => {

secondValue.push(item["householdname"]);

});

pm.test("Succes compared vestmark view household_name values are present in salesforce view householdname organization = TPI ", function () {

for (var i = 0, len =pm.environment.get(ā€˜Sizeā€™); i < len; i++){

if(pm.expect(secondValue).to.include(pm.collectionVariables.get(`one-${i}`)));

}

});

can we do it any other simple way ?

var initial_resp = {
ā€œnameā€: ā€œi_vestmark_tpi_household_allocationsā€,
ā€œelementsā€: [
{
ā€œhousehold_nameā€: ā€œTest Household DEFā€
},
{
ā€œhousehold_nameā€: ā€œtestā€
},
{
ā€œhousehold_nameā€: ā€œTest Household 3ā€
},
{
ā€œhousehold_nameā€: ā€œTest WMā€
},
{
ā€œhousehold_nameā€: ā€œTestqwewq WMā€
}]

}

var later_response = {
ā€œnameā€: ā€œi_source_data_salesforce_tpi_target_allo_v2ā€,
ā€œelementsā€: [
{
ā€œhouseholdnameā€: ā€œTest Household DEFā€,
ā€œorganizationā€: ā€œTPIā€
},
{
ā€œhouseholdnameā€: ā€œtestā€,
ā€œorganizationā€: ā€œTPIā€
},
{
ā€œhouseholdnameā€: ā€œTest Household 3ā€,
ā€œorganizationā€: ā€œTPIā€
},
{
ā€œhouseholdnameā€: ā€œTest WMā€,
ā€œorganizationā€: ā€œTPIā€
}]

}

var household_name_array= initial_resp.elements.map(function (element) { return element.household_name; });

var var householdname_array= later_response .elements.map(function (element) { return element.householdname; });

var check = false;

var check = init_map.every((el) => {
return response_map.indexOf(el) !== -1;
});

//check will be true if later response has all the values from initial response

Hope this works for you.

var later_response = {
    "name": "i_source_data_salesforce_tpi_target_allo_v2",
    "elements": [
        {
            "householdname": "Test Household DEF",
            "organization": "TPI"
        },
        {
            "householdname": "test",
            "organization": "TPI"
        },
        {
            "householdname": "Test Household 3",
            "organization": "TPI"
        },
        {
            "householdname": "Test WM",
            "organization": "TPI"
        }]
        
}

var initial_resp = {
    "name": "i_vestmark_tpi_household_allocations",
    "elements": [
        {
            "household_name": "Test Household DEF"
        },
        {
            "household_name": "test"
        },
        {
            "household_name": "Test Household 3"
        },
        {
            "household_name": "Test WM"
        }
}
var household_name_array= initial_resp.elements.map(function (element) { return element.household_name; });

var householdname_array=  later_response .elements.map(function (element) { return element.householdname; });

var check = false;

var check = household_name_array.every((el) => {
	return householdname_array.indexOf(el) !== -1;
});

//check will be true if later response has all the values from initial response
1 Like

Got it thanksā€¦

I have used like below

First TAB
// store vestmark list in variable

let jsonData = pm.response.json().elements

var household_name_array = ;

pm.test(ā€œsuccess household_name column stored as variable vestmarkListā€, function () {

household_name_array= jsonData.map(function (elements) { return elements.household_name; });

pm.collectionVariables.set(vestmarkList, JSON.stringify(household_name_array));

});

Second TAB

// Validate Vestmark values are in Salesforce table

let jsonData = pm.response.json().elements;

var vestMarkList = JSON.parse(pm.collectionVariables.get(vestmarkList));

var salesforceList = jsonData.map(function (element) {return element.householdname});

var check = false;

pm.test("Succes compared vestmark view household_name values are present in salesforce view householdname organization = TPI ", function () {

check = vestMarkList.forEach((el) => {

    return salesforceList.indexOf(el) !== -1;

            

})

});