Assert that the JSONs array contains one property
I have a device storage project that allows you to add / remove / edit devices and I am trying to check in postman that after a POST that adds a device to the list, the device can be found in my response body. I am using Postman BDD and Chai sintax
[
{
"deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
"name": "Huawei",
"alias": "electronics",
"quantity": 10,
"price": 200,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "90807800-c66c-46ec-ae46-687464e62797",
"name": "Pixel 2",
"alias": "electronics",
"quantity": 10,
"price": 300,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
"name": "SONYk",
"alias": "electronicsm",
"quantity": 122,
"price": 2222,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
"name": "ASUS1",
"alias": "electronics",
"quantity": 1222,
"price": 2222,
"categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
"links": []
}
]
This is my answer as you can see that it is returning a JSON array and I am looking for an elegant way to assert that the device with the "Pixel 2" property is in the array. I am open to any other javascript libraries that can help me achieve this goal.
source to share
You can use a method find
that takes a callback function .
var array=[
{
"deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
"name": "Huawei",
"alias": "electronics",
"quantity": 10,
"price": 200,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "90807800-c66c-46ec-ae46-687464e62797",
"name": "Pixel 2",
"alias": "electronics",
"quantity": 10,
"price": 300,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
"name": "SONYk",
"alias": "electronicsm",
"quantity": 122,
"price": 2222,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
"name": "ASUS1",
"alias": "electronics",
"quantity": 1222,
"price": 2222,
"categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
"links": []
}];
var item=array.find(function(item){
return item.name=="Pixel 2";
});
console.log(item);
console.log("Exists: "+item!=undefined);
Another method is to use a function includes
.
var array=[
{
"deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
"name": "Huawei",
"alias": "electronics",
"quantity": 10,
"price": 200,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "90807800-c66c-46ec-ae46-687464e62797",
"name": "Pixel 2",
"alias": "electronics",
"quantity": 10,
"price": 300,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
"name": "SONYk",
"alias": "electronicsm",
"quantity": 122,
"price": 2222,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
"name": "ASUS1",
"alias": "electronics",
"quantity": 1222,
"price": 2222,
"categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
"links": []
}];
var exists=array.map(function(item){
return item.name;
}).includes("Pixel 2");
console.log(exists);
source to share
No library required. You can use Array.prototype.some
:
if (response.some(i => i.name == 'Pixel 2')) {
// ...
}
source to share
Use Array#filter
with Array#length
to see if your item exists
const array = [{
"deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
"name": "Huawei",
"alias": "electronics",
"quantity": 10,
"price": 200,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "90807800-c66c-46ec-ae46-687464e62797",
"name": "Pixel 2",
"alias": "electronics",
"quantity": 10,
"price": 300,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
"name": "SONYk",
"alias": "electronicsm",
"quantity": 122,
"price": 2222,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
"name": "ASUS1",
"alias": "electronics",
"quantity": 1222,
"price": 2222,
"categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
"links": []
}
];
const hasPixel2 = array.filter(item => item.name === 'Pixel 2').length > 0;
console.log(hasPixel2);
source to share
Or Array#Filter
var array=[
{
"deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
"name": "Huawei",
"alias": "electronics",
"quantity": 10,
"price": 200,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "90807800-c66c-46ec-ae46-687464e62797",
"name": "Pixel 2",
"alias": "electronics",
"quantity": 10,
"price": 300,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
"name": "SONYk",
"alias": "electronicsm",
"quantity": 122,
"price": 2222,
"categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
"links": []
},
{
"deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
"name": "ASUS1",
"alias": "electronics",
"quantity": 1222,
"price": 2222,
"categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
"links": []
}];
var item = array.filter(item => item.name === "Pixel 2");
console.log(item);
source to share