How to check datatype of json data or response data whose value is null in array[0] and value number in array[1]

Hi everyone,

I am just writing the json or response what I got and need to check the data type of it …pls check below json


{
	"size": 2,
	"response": [
		{
			"alert_id": "00000000000",
			"tenant_id": "abc1234",
			"history_id": "0000000000000",
			"recorded_time": 00000000000,
			"history_type": "STATE_CHANGE",
			"alert_snapshot": {
				"tenant_id": "abc1234",
				"alert_id": "00000000000000",
				"alert_status": "OPEN",
				"prev_alert_status": "OPEN",
				"severity": "CRITICAL",
				"alert_type": "abcsdefghh",
				"alert_score": 100,
				"entity": {
					"entity_id": "abc.com",
					"entity_type": "aaaaa",
					"entity_group": "aaaaaaa"
				},
				"create_time": 0000000000000,
				"update_time": 0000000000000,
				"message": "aaaaaaaaa",
				"confidence_score": 100,
				"assigned_to": null,
				"recommendation": "aaaaaaaaaaaaaaaaaaaa",
				"user_name": null,
				"event_origin_list": [
					"NETWORK"
				]
			},
			"user_action": null
		},
		{
			"alert_id": "00000000000",
			"tenant_id": "abc1234",
			"history_id": "0000000000000",
			"recorded_time": 00000000000,
			"history_type": "STATE_CHANGE",
			"alert_snapshot": {
				"tenant_id": "abc1234",
				"alert_id": "00000000000000",
				"alert_status": "OPEN",
				"prev_alert_status": "OPEN",
				"severity": "CRITICAL",
				"alert_type": "abcsdefghh",
				"alert_score": 100,
				"entity": {
					"entity_id": "abc.com",
					"entity_type": "aaaaa",
					"entity_group": "aaaaaaa"
				},
				"create_time": 0000000000000,
				"update_time": 0000000000000,
				"message": "aaaaaaaaa",
				"confidence_score": 100,
				"assigned_to": null,
				"recommendation": "aaaaaaaaaaaaaaaaaaaa",
				"user_name": null,
				"event_origin_list": [
					"NETWORK"
				]
			},
			"user_action": {
				"action_time": 000000000000000,
				"user_name": "abcd",
				"user_action": "dfghnjk",
				"is_successful": null,
				"action_details": "",
				"notes": "sdcfvgbhjnk"
			}
		}
	]
}

to elaborate on the problem I have is that whenever I am checking the datatype of

for (i = 0; i < jsonData.response.length; i++) {
pm.expect(jsonData.response[i].user_action.action_time).to.be.a("number"); 
}

Then it shows the error of FAIL

Datatype Check | TypeError: Cannot read property ‘action_time’ of null

As you can see the first array index value has null as a response body of array index[0} and if u see array index[1] u will see there is a value of x.response[1].user_action.action_time with some number and same as x.response[0].user_action.action_time does not have any value as x.response[0].user_action value is blank in the array first value.

Here I am using for loop to check each response of the json using iteration hence can not find the solution of the problem… can someone help??

In short, I have to check the data type of json array’s values which has null and also some value in other array indexes
@danny-dainton sir can u also help here

Something like this maybe:

pm.test("Check datatype", () => {
    _.each(jsonData.response, (item) => {
        if(item.user_action !== null) {
            pm.expect(item.user_action.action_time).to.be.a("number");
        }
    })
})