Compare two Json responses its have same array and different objects and same values

Hello Everyone ,

Can someone help me here please

compare two Json responses its have same array and different objects and same values

1st Response

{
    "name": "i_vestmark_tpi_household_allocations",
    "elements": [
        {
            "household_name": "Test Household DEF"
        },
        {
            "household_name": "test"
        },
        {
            "household_name": "Test Household 3"
        },
        {
            "household_name": "Test WM"
        }]
        
}

2nd 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"
        }]
        
}

its have array , in 2 responses objects are different but values are same, I need to compare both object values are same

console.log(_.isEqual(a,b))

use lodash isEqual Method

where a and b are the two requests

in first Response I have only one object
and in second Response I have two objects
first Response Object and Second Response Object are different but values are same , I need to compare those

a= a.elements.map((val,index)=>val.householdname)
b= b.elements.map((val,index)=>val.householdname)

get only house hold names from a and b and comapre it using isEqual

_.isEqual(a,b)

Map will iterate through each element in the elements property and returns the householdname value.

(val,index)=>val.householdname is a lambda function that returns val.housholdname

its same as

    (val,index)=>{ return val.householdname}

@praveendvd my objects are different

a = {

    "name": "i_vestmark_tpi_household_allocations",

    "elements": [

        {

            "household_name": "Test Household DEF"

        },

        {

            "household_name": "test"

        },

        {

            "household_name": "Test Household 3"

        },

        {

            "household_name": "Test WM"

        }]

}

b =

{

    "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"

        }]

}

a= a.elements.map((val,index)=>val.household_name)

b= b.elements.map((val,index)=>val.householdname)

console.log(a,b)

console.log(_.isEqual(a,b))
1 Like

Thank you …

1 Like