# Trying to assert empty/null/undefined value with no luck

**URL:** <https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958>\
**Category:** Help Hub\
**Tags:** responseassertion\
**Created:** [October 21, 2021, 1:27pm UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958 "2021-10-21T13:27:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amy6586](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/amy6586/32/10138_2.png) [@amy6586](https://community.postman.com/u/amy6586)\
**Post date:** [October 21, 2021, 1:27pm UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/1 "2021-10-21T13:27:55Z")

</div>

Hello!  
I’m fairly new to Postman and I’m not a Mocha expert, I’m using the “Using CSV” data to load a file with the data I need and that is working perfectly, my problem is that I need to assert one of the fields and I had research a lot in Google, StackOverflow, this forum and nothing ☹

This is the sample response I get:

```auto
[
    {
        "airlineCode": "OLY",
        "airlineCountryCode": "GR",
        "airlineName": "Olympus",
        "arrivalActualDateTime": "2018-07-04T00:37:00",
        "arrivalAirport": "Tenerife Sur Airport (TFS)",
        "arrivalAirportCode": "TFS",
        "arrivalFlightStatus": "Arrived",
        "arrivalFlightStatus2": "Arrived",
        "arrivalScheduledDateTime": "2018-07-03T21:45:00",
        "departureActualDateTime": "2018-07-03T20:00:00",
        "departureAirport": "Hamburg Airport (HAM)",
        "departureAirportCode": "HAM",
        "departureFlightStatus": "Departed",
        "departureFlightStatus2": "Departed",
        "departureScheduledDateTime": "2018-07-03T17:35:00",
        "distance": 3577,
        "flightCode": "OLY246",
        "flightDate": "2018-07-03",
        "flightLegId": 189543873,
        "flightNumber": "246",
        "isArrivalActualDateTimeCalculated": false,
        "isArrivalScheduledDateTimeCalculated": false,
        "isDepartureActualDateTimeCalculated": false,
        "isDepartureScheduledDateTimeCalculated": false,
        "operatingAirlineCode": "OLY",
        "operatingCarrierCode": null,
        "operatingFlightNumber": 246,
        "problemFlightId": null,
        "scheduleId": null
    }
]

```

From that response, I wanted to assert that the ` "operatingCarrierCode"` is not null or empty so I can cherry-pick from more than 2400 calls the ones that actually have a real value there.  
I had tried the following code with no luck:  
I know that the field type is “undefined” when I ran some code I found to check it.

```auto
//pm.test("To Check if Value is Null", function() {
var jsonData = pm.response.json();
//pm.expect(jsonData.operatingCarrierCode).not.eq('');
//pm.expect(jsonData.operatingCarrierCode).toBeNull();
//expect(context).toBeDefined();
//expect(context).not.toBeNull();
//pm.expect(jsonData.operatingCarrierCode).not.eq(NaN);
//});

//pm.test("operatingCarrierCode present", function() {
   //console.log(typeof jsonData.operatingCarrierCode);
   //pm.expect(jsonData.operatingCarrierCode).to.exist
   //pm.expect(jsonData.operatingCarrierCode).to.not.be.null
   //pm.expect(jsonData.operatingCarrierCode).to.include("null");
//});

pm.test("Body matches string", function () 
{
    pm.expect(pm.jsonData.text()).to.not.include("null");
});

```

Can anyone be so kind to let me know how to do that? Thanks 😃

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 21, 2021, 1:39pm UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/2 "2021-10-21T13:39:45Z")

</div>

Hey @amy6586 👋

The response is an array so you would need to use `jsonData[0].operatingCarrierCode` to reference the object within it.

As for assertions, Postman uses chai.js for this and more info on what could be used can be found here:

> **[Expect / Should - Chai](https://www.chaijs.com/api/bdd/)**
>
> Chai is a BDD / TDD assertion library for \[node\](http://nodejs.org) and the browser that can be delightfully paired with any javascript testing framework.

There are some test examples here on our learning center that also might help:

> **[Test script examples | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/)**
>
> Test script examples: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

This is also a similar issue covered by @vdespa in one of his YouTube videos:

[![](https://img.youtube.com/vi/5u8Rv-gUR-M/maxresdefault.jpg "Avoid these 10 Beginner Mistakes in Postman") ](https://www.youtube.com/watch?v=5u8Rv-gUR-M)

---

<div class="post-metadata">

**Author:** ![amy6586](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/amy6586/32/10138_2.png) [@amy6586](https://community.postman.com/u/amy6586)\
**Post date:** [October 21, 2021, 2:35pm UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/3 "2021-10-21T14:35:55Z")

</div>

hi @danny-dainton,

Thanks for your reply I had tried the following piece of code but is not working as expected.

```auto
pm.test("Verify the operatingCarrierCode is not null" , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].operatingCarrierCode, 'operatingCarrierCode is not available').to.not.be.undefined;
});

```

In my JSON the field **`operatingCarrierCode`** comes as null and sometimes it will have an airline code and checking for undefined does not work I had tried using something like:

```auto
pm.test("Verify the operatingCarrierCode is not null" , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].operatingCarrierCode, 'operatingCarrierCode is not available').to.not.be.undefined;
});

```

![Screenshot 2021-10-21 at 16.36.14](https://us1.discourse-cdn.com/getpostman/original/2X/c/c4630082bd97c5c7161ae08eaa9d19d6ba42c42a.png)

but the response is still shown as null and the assertion passes, I want to check if the response contains something different as null it passes.

if I use something like this:

`ppm.expect(pm.jsonData[0].operatingCarrierCode).to.not.include("null");`

I get an error

‘TypeError: Cannot read property ‘0’ of undefined’

any advice?

thanks 🙂

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 21, 2021, 3:09pm UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/4 "2021-10-21T15:09:04Z")

</div>

If it’s not `null` then it’s a `string` with the code, that wouldn’t be `undefined` unless the whole property was removed from the response.

Does this not work? If it is `null` or any other type then it will fail.

```auto
pm.test("Verify the operatingCarrierCode is returned" , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].operatingCarrierCode, 'operatingCarrierCode is not available').to.be.a('string');
});

```

Ideally, you would be asserting against an actual known value in the response, as this would pass if the carrier code was `Bananas` or some other string value.

Maybe this was a typo but none of that really makes sense to me. You wouldn’t use `pm.jsonData` and the `to.not.include()` wouldn’t work like that, it would also be looking for a string containing null and not the datatype.

> [@amy6586](#):
>
> pm.expect(pm.jsonData[0].operatingCarrierCode).to.not.include(“null”

---

<div class="post-metadata">

**Author:** ![amy6586](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/amy6586/32/10138_2.png) [@amy6586](https://community.postman.com/u/amy6586)\
**Post date:** [October 22, 2021, 7:12am UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/5 "2021-10-22T07:12:49Z")

</div>

Thanks @danny-dainton that works like a charm, unfortunately, this value can be any airline. code so there are 100s of possibilities but checking if it is a string works very well.

🙂

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 22, 2021, 8:20am UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/6 "2021-10-22T08:20:18Z")

</div>

You mentioned that you’re using a CSV file, does this contain those codes?

Or are you using the data in the file for something else?

---

<div class="post-metadata">

**Author:** ![amy6586](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/amy6586/32/10138_2.png) [@amy6586](https://community.postman.com/u/amy6586)\
**Post date:** [October 22, 2021, 8:56am UTC](https://community.postman.com/t/trying-to-assert-empty-null-undefined-value-with-no-luck/28958/7 "2021-10-22T08:56:23Z")

</div>

@danny-dainton the CSV is only to change the date and flight number in the query parameters
