Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert that a Array of JSONs contains one property

I have a device store project that allows to add/delete/edit devices and I'm trying to test in postman that after a POST ,that adds a device to the list ,the device can be found in my response body.I'm using Postman BDD and the 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 response , as it can be seen it return a Array of JSONs and I'm looking for an elegant way to assert that the device with the property "Pixel 2" is in the Array. I'm open to any other javascript librarys that can help me achieve this thing.

like image 961
T.Frincu Avatar asked Jan 28 '26 02:01

T.Frincu


1 Answers

You can use find method which accepts a callback provided 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 includes 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 exists=array.map(function(item){
  return item.name;
}).includes("Pixel 2");
console.log(exists);
like image 153
Mihai Alexandru-Ionut Avatar answered Jan 29 '26 14:01

Mihai Alexandru-Ionut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!