# AssertionError to check keys in json

**URL:** <https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921>\
**Category:** Help Hub\
**Tags:** tests, json, array\
**Created:** [August 5, 2022, 10:44am UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921 "2022-08-05T10:44:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![science-pilot-863855](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@science-pilot-863855](https://community.postman.com/u/science-pilot-863855)\
**Post date:** [August 5, 2022, 10:44am UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921/1 "2022-08-05T10:44:49Z")

</div>

I have an API Endpoint that returns a JSON array in response:

```auto
[
    {
        "base": "ethereum-eth",
        "quote": "tether-usdt",
        "volume": 60126390.25608005,
        "price": 1663.0597462528697,
        "volumePercent": 32.36999258988893,
        "spread": 0.006014675808968427,
        "category": "Verified",
        "time": 1659695310
    },
]

```

I try to run the code below for each item of Array

```auto
pm.test("Check if response contains all the keys", function () {
    var data = pm.response.json();
    data.forEach(function(item) {
        pm.expect(item).to.have.all.keys('base', 'qoute', 'volume', 'price', 'volumePercent', 'spread', 'category', 'time');     
    });
});

```

Response

```auto
Check if response contains all the keys | AssertionError: expected { base: 'ethereum-eth', …(7) } to have keys 'base', 'qoute', 'volume', 'price', 'volumePercent', 'spread', 'category', and 'time'

```

For some reason, even for one item, I have an Assertion error

```auto
pm.test("Check if response contains all the keys", function () {
    var data = pm.response.json();
    console.log(data[0]);
    pm.expect(data[0]).to.have.all.keys('base', 'qoute', 'volume', 'price', 'volumePercent', 'spread', 'category', 'time');
});

```

**How I can resolve it?** pls, help

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 5, 2022, 8:05pm UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921/2 "2022-08-05T20:05:17Z")

</div>

Hi @science-pilot-863855

The easiest way to achieve this is to have multiple assertions, like this;

```auto
const response = pm.response.json();

pm.test("Check if response contains all the keys", function () {
    pm.expect(response).to.have.property('base');
    pm.expect(response).to.have.property('quote');
    pm.expect(response).to.have.property('volume');
    pm.expect(response).to.have.property('price');
    pm.expect(response).to.have.property('volumePercent');
    pm.expect(response).to.have.property('spread');
    pm.expect(response).to.have.property('category');
    pm.expect(response).to.have.property('time');
})

```

* * *

However, if it is necessary to run a for loop, then you could do this;

```auto
const response = pm.response.json();

//get all of the keys from the response as an array
let arrayOfResponseKeys = Object.keys(response);
console.log(arrayOfResponseKeys);

//create an array with what you expect the keys to be
let myKeysList = ['base', 'quote', 'volume', 'price', 'volumePercent', 'spread', 'category', 'time'];
console.log(myKeysList);

pm.test("Check if response contains all the keys with for loop", function () {
    //loop through the response array
    for (let x = 0; x < response.length; x++) {
        //for each item in the response array, loop through the entire array of keys
        for (let y = 0; y < myKeysList.length; y++) {
            //match the response array to the expected array
            pm.expect(arrayOfResponseKeys[y]).to.include(myKeysList[y]);
        }
    }
})

```

Note: you also had a spelling mistake that threw me off a little, your second key was spelt ‘qoute’. Thought it was worth mentioning just in case it caused any further issues.

---

<div class="post-metadata">

**Author:** ![science-pilot-863855](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@science-pilot-863855](https://community.postman.com/u/science-pilot-863855)\
**Post date:** [August 8, 2022, 9:33am UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921/3 "2022-08-08T09:33:13Z")

</div>

Thanks, I thought about some spelling mistakes and I checked everything a few times. lol, it was so simple.  
And also thank you for advice, I found a validation schema that is also easy to use: [https://postman-quick-reference-guide.readthedocs.io/en/latest/schema-validation.html#object-has-required-property](https://postman-quick-reference-guide.readthedocs.io/en/latest/schema-validation.html#object-has-required-property)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 16, 2022, 4:00pm UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921/4 "2022-08-16T16:00:41Z")

</div>

Shouldn’t these be multiple tests, rather than multiple assertions in the same test.

If the first assertion fails, then the rest of the assertions\tests don’t run. Therefore if multiple properties are missing, you aren’t necessarily going to see that in the results, making troubleshooting the issue more difficult.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 16, 2022, 6:53pm UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921/5 "2022-08-16T18:53:16Z")

</div>

If you use try/catch inside the test it would go through all of the properties regardless of failures. The result would be a full list of passed and failed assertions inside a singular test.

Better yet, use schema validation.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 17, 2022, 8:33am UTC](https://community.postman.com/t/assertionerror-to-check-keys-in-json/37921/6 "2022-08-17T08:33:51Z")

</div>

I was going to say that, to use the schema validation as shown in the learning\training modules.
