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

**URL:** <https://community.postman.com/t/verify-the-first-json-response-objects-length-are-same-with-second-json-response-objects-length/20753>\
**Category:** Help Hub\
**Tags:** tests, variables, collections\
**Created:** [February 24, 2021, 6:02pm UTC](https://community.postman.com/t/verify-the-first-json-response-objects-length-are-same-with-second-json-response-objects-length/20753 "2021-02-24T18:02:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhasker.vade](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@bhasker.vade](https://community.postman.com/u/bhasker.vade)\
**Post date:** [February 24, 2021, 6:02pm UTC](https://community.postman.com/t/verify-the-first-json-response-objects-length-are-same-with-second-json-response-objects-length/20753/1 "2021-02-24T18:02:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [February 25, 2021, 12:36pm UTC](https://community.postman.com/t/verify-the-first-json-response-objects-length-are-same-with-second-json-response-objects-length/20753/2 "2021-02-25T12:36:51Z")

</div>

In the tests of your first request you can add

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

```

In the tests of your second request you can add

```auto
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!

---

<div class="post-metadata">

**Author:** ![bhasker.vade](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@bhasker.vade](https://community.postman.com/u/bhasker.vade)\
**Post date:** [February 26, 2021, 3:41pm UTC](https://community.postman.com/t/verify-the-first-json-response-objects-length-are-same-with-second-json-response-objects-length/20753/3 "2021-02-26T15:41:25Z")

</div>

Used this snippets , it worked , thank you 🙂

---

<div class="post-metadata">

**Author:** ![bhasker.vade](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@bhasker.vade](https://community.postman.com/u/bhasker.vade)\
**Post date:** [February 26, 2021, 6:11pm UTC](https://community.postman.com/t/verify-the-first-json-response-objects-length-are-same-with-second-json-response-objects-length/20753/4 "2021-02-26T18:11:28Z")

</div>

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);

});
