AssertionError to check keys in json

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

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

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

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

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

Hi @science-pilot-863855

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

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;

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.

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