Verify the first Json response Objects Length are same with Second Json response Objects Length

Hi Team,

can someone help me here

verify the first Json response Objects length are same with Second Json response Objects length

First Json Response
{

"name": "i_idw_tpi_port_tran_qtrly",

"elements": [

    {

        "transactionid": "20124099"

    },

    {

        "transactionid": "20077005"

    },

    {

        "transactionid": "20128098"

    },

    {

        "transactionid": "20124173"

    },

    {

etc… it have 50000

Second Json Response

{

"name": "dv_source_data_marty_managed_accounts_fact_transaction",

"elements": [

    {

        "trade_date": "2020-11-30",

        "links": [

            {

                "rel": "self",

                "href": "https://ldw-dev.dev.schwab.com:9443/denodo-restfulws/sams_ldw_managed_accounts/views/dv_source_data_marty_managed_accounts_fact_transaction/18847499"

            }

        ]

    },

    {

        "trade_date": "2020-11-30",

        "links": [

            {

                "rel": "self",

                "href": "https://ldw-dev.dev.schwab.com:9443/denodo-restfulws/sams_ldw_managed_accounts/views/dv_source_data_marty_managed_accounts_fact_transaction/18847500"

            }

        ]

    },

    {

        "trade_date": "2020-11-30",

        "links": [

            {

                "rel": "self",

                "href": "https://ldw-dev.dev.schwab.com:9443/denodo-restfulws/sams_ldw_managed_accounts/views/dv_source_data_marty_managed_accounts_fact_transaction/18847502"

            }

        ]

    },

    {

        "trade_date": "2020-11-30",

        "links": [

            {

                "rel": "self",

                "href": "https://ldw-dev.dev.schwab.com:9443/denodo-restfulws/sams_ldw_managed_accounts/views/dv_source_data_marty_managed_accounts_fact_transaction/18847504"

            }

        ]

    },

    {

        "trade_date": "2020-11-30",

        "links": [

            {

                "rel": "self",

                "href": "https://ldw-dev.dev.schwab.com:9443/denodo-restfulws/sams_ldw_managed_accounts/views/dv_source_data_marty_managed_accounts_fact_transaction/18847506"

            }

        ]

    },

    {

etc…it have 50000

from the first response Array is elements having 50000 objects name transactionid

from the Second response Array is elements having 50000 objects name trade_date

we have to store the length of the first Array in variable and verify the length with second Array

In the tests of your first request you can add

const jsonData = pm.response.json();
pm.variables.set('transactionIdCount', jsonData.elements.length);

In the tests of your second request you can add

const transactionIdCount = pm.variables.get('transactionIdCount');
const jsonData = pm.response.json();
pm.test('Lengths are the same', function(){
  pm.expect(jsonData.elements.length).to.equal(transactionIdCount);
});

What these snippets are doing are first setting the number of elements in the array from your first response. Then in the second request it is comparing the number of objects in the array with the stored off value from the first request.

Hope this helps!

2 Likes

Used this snippets , it worked , thank you :slight_smile:

1 Like

I have used Below Snippets

First Request
pm.test("Verify the records ",function()

{

var response = JSON.parse(responseBody);

var firstResultLength = Object.keys(response.elements).length

pm.environment.set(“columnLength”, firstResultLength)

// console.log(columnLength);

});

Second Request
pm.test("Verify the records ",function()

{

var response = JSON.parse(responseBody);

var secondResultLength = Object.keys(response.elements).length

console.log(“2nd” + secondResultLength);

var firstResultLength = pm.collectionVariables.get(“columnLength”);

console.log(“1st” + firstResultLength);

// console.log(secondResultLength)

pm.expect(firstResultLength).to.eql(secondResultLength);

});