JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
975 stars 175 forks source link

jsonPath: Cannot read properties of null #161

Closed 36ve closed 3 weeks ago

36ve commented 3 years ago

Describe the bug

jsonPath: Cannot read properties of null

Code sample or steps to reproduce

// package.json

{
  "name": "jsonpath-issue",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "jsonpath-plus": "^6.0.1"
  }
}
// index.js

const { JSONPath } = require("jsonpath-plus");

const json = [
  {
    AlarmId: 22319514,
    AgentId: 698701387007140.0,
    agentName: "bkpcseprdcw01",
    MonitorAlarmStateId: 1,
    AlarmSubject: "Memory ALARM bkpcseprdcw01.root.fbd",
    Message:
      "Memory bkpcseprdcw01.root.fbd\r\n\r\nSNMP Device: N/A\r\nMonitor Set: UPSTREAM - BASIC - Performance - Memory Usage - Servers ENG\r\nType: Counter\r\nLog Object Name: Memory\r\nAgent Alarm Time: 4:08:01 pm 29-Sep-21\r\nEvent Time: 3:08:01 pm 29-Sep-21 UTC\r\nLog Value: 99.354195\r\nAlarm Operator: Over\r\nAlarm Threshold: 95 % Committed Bytes In Use\r\nAlarm Duration: 1 Hour(s)\r\n",
    AlarmType: null,
    MonitorType: 0,
    EventTime: "2021-09-29T15:08:01.373",
    AlertType: 0,
    MonitorSet: " UPSTREAM - BASIC - Performance - Memory Usage - Servers ENG",
    LogObjectName: " Memory",
    LogValue: " 99.354195",
  },
  {
    AlarmId: 22260562,
    AgentId: 968066020621128.0,
    agentName: "fbdserprd02",
    MonitorAlarmStateId: 1,
    AlarmSubject: "Processor::% Processor Time ALARM fbdserprd02.root.fbd",
    Message:
      "Processor::% Processor Time fbdserprd02.root.fbd\r\n\r\nSNMP Device: N/A\r\nMonitor Set: Windows (Core) - Processor and Memory Performance\r\nType: Counter\r\nLog Object Name: Processor::% Processor Time\r\nAgent Alarm Time: 1:31:31 am 29-Sep-21\r\nEvent Time: 12:31:31 am 29-Sep-21 UTC\r\nLog Value: 81.231064\r\nAlarm Operator: Over\r\nAlarm Threshold: 80 % Processor Time\r\nAlarm Duration: 1 Hour(s)\r\n",
    AlarmType: null,
    MonitorType: 0,
    EventTime: "2021-09-29T00:31:31.96",
    AlertType: 0,
    MonitorSet: " Windows (Core) - Processor and Memory Performance",
    LogObjectName: " Processor::% Processor Time",
    LogValue: " 81.231064",
  },
  {
    AlarmId: 22233278,
    AgentId: 874213296083191.0,
    agentName: "fbddpstndprd01",
    MonitorAlarmStateId: 1,
    AlarmSubject: "Processor::% Processor Time ALARM fbddpstndprd01.root.fbd",
    Message:
      "Processor::% Processor Time fbddpstndprd01.root.fbd\r\n\r\nSNMP Device: N/A\r\nMonitor Set: Windows (Core) - Processor and Memory Performance\r\nType: Counter\r\nLog Object Name: Processor::% Processor Time\r\nAgent Alarm Time: 7:07:34 pm 28-Sep-21\r\nEvent Time: 6:07:34 pm 28-Sep-21 UTC\r\nLog Value: 100\r\nAlarm Operator: Over\r\nAlarm Threshold: 80 % Processor Time\r\nAlarm Duration: 1 Hour(s)\r\n",
    AlarmType: null,
    MonitorType: 0,
    EventTime: "2021-09-28T18:07:34.083",
    AlertType: 0,
    MonitorSet: " Windows (Core) - Processor and Memory Performance",
    LogObjectName: " Processor::% Processor Time",
    LogValue: " 100",
  },
  {
    AlarmId: 22233277,
    AgentId: 275001748225221.0,
    agentName: "fbddprdod_02",
    MonitorAlarmStateId: 1,
    AlarmSubject: "Processor::% Processor Time ALARM fbddprdod_02.root.fbd",
    Message:
      "Processor::% Processor Time fbddprdod_02.root.fbd\r\n\r\nSNMP Device: N/A\r\nMonitor Set: Windows (Core) - Processor and Memory Performance\r\nType: Counter\r\nLog Object Name: Processor::% Processor Time\r\nAgent Alarm Time: 7:07:32 pm 28-Sep-21\r\nEvent Time: 6:07:32 pm 28-Sep-21 UTC\r\nLog Value: 100\r\nAlarm Operator: Over\r\nAlarm Threshold: 80 % Processor Time\r\nAlarm Duration: 1 Hour(s)\r\n",
    AlarmType: null,
    MonitorType: 0,
    EventTime: "2021-09-28T18:07:32.183",
    AlertType: 0,
    MonitorSet: " Windows (Core) - Processor and Memory Performance",
    LogObjectName: " Processor::% Processor Time",
    LogValue: " 100",
  },
  {
    AlarmId: 22233276,
    AgentId: 824606191757140.0,
    agentName: "fbddprdod_01",
    MonitorAlarmStateId: 1,
    AlarmSubject: "Processor::% Processor Time ALARM fbddprdod_01.root.fbd",
    Message:
      "Processor::% Processor Time fbddprdod_01.root.fbd\r\n\r\nSNMP Device: N/A\r\nMonitor Set: Windows (Core) - Processor and Memory Performance\r\nType: Counter\r\nLog Object Name: Processor::% Processor Time\r\nAgent Alarm Time: 7:06:27 pm 28-Sep-21\r\nEvent Time: 6:06:27 pm 28-Sep-21 UTC\r\nLog Value: 100\r\nAlarm Operator: Over\r\nAlarm Threshold: 80 % Processor Time\r\nAlarm Duration: 1 Hour(s)\r\n",
    AlarmType: null,
    MonitorType: 0,
    EventTime: "2021-09-28T18:06:27.727",
    AlertType: 0,
    MonitorSet: " Windows (Core) - Processor and Memory Performance",
    LogObjectName: " Processor::% Processor Time",
    LogValue: " 100",
  },
];

const result = JSONPath({ path: "$..[?(@.MonitorAlarmStateId != 0 && @.MonitorType == 4)].AlarmId", json });

console.log(result);
node index.js

Console error or logs

Expected behavior

It should be able to return Data noted by that JSONPath however it will error out as the Query section appears to break it. It will function when there is no Query Section.

Expected result

The full JSONPath with query section will work.

Environment (IMPORTANT)

Additional context

brettz9 commented 3 years ago

I don't know what the current "standard" behavior is, but you can work around this with:

"$..[?(@ && @.MonitorAlarmStateId != 0 && @.MonitorType == 4)].AlarmId"
JXGA commented 2 years ago

I'm also encountering this issue. Workaround from @brettz9 fixed this issue.

80avin commented 4 weeks ago

It is fixed in 9.0.0 using ignoreEvalErrors

Check: https://github.com/JSONPath-Plus/JSONPath/issues/218#issuecomment-2383950924