使用 javascript 预处理 Zabbix 提取 json

extract json with javascript preprocessing Zabbix

我正在尝试推断 json 是否包含字符串。我的 json 是这样的:

[
   {
      "name":"PE02S-PK00400_2002",
      "ip":"",
      "pk":0.4,
      "roadCode":"A32",
      "roadName":"A32",
      "roadDescription":"A32 TORINO-BARDONECCHIA",
      "directionName":"A32_DIR_A",
      "directionDescription":"BARDONECCHIA",
      "sectionName":"A32_DIR_A_SEZ1",
      "sectionDescription":"RIVOLI - SUSA",
      "laneName":null,
      "laneDescription":null,
      "tunnelName":"A32_GALLERY_LA_PEROSA",
      "tunnelDescription":"LA PEROSA",
      "idTecnico":"2002"
   },
   {
      "name":"PE04S-PK00500_2004",
      "ip":"",
      "pk":0.5,
      "roadCode":"A32",
      "roadName":"A32",
      "roadDescription":"A32 TORINO-BARDONECCHIA",
      "directionName":"A32_DIR_A",
      "directionDescription":"BARDONECCHIA",
      "sectionName":"A32_DIR_A_SEZ1",
      "sectionDescription":"RIVOLI - SUSA",
      "laneName":null,
      "laneDescription":null,
      "tunnelName":"A32_GALLERY_LA_PEROSA",
      "tunnelDescription":"LA PEROSA",
      "idTecnico":"2004"
   },
   {
      "name":"PE06S-PK00750_2006",
      "ip":"",
      "pk":0.75,
      "roadCode":"A32",
      "roadName":"A32",
      "roadDescription":"A32 TORINO-BARDONECCHIA",
      "directionName":"A32_DIR_A",
      "directionDescription":"BARDONECCHIA",
      "sectionName":"A32_DIR_A_SEZ1",
      "sectionDescription":"RIVOLI - SUSA",
      "laneName":null,
      "laneDescription":null,
      "tunnelName":"A32_GALLERY_LA_PEROSA",
      "tunnelDescription":"LA PEROSA",
      "idTecnico":"2006"
   }
]

javascript脚本是这样的:

function (value) {
    var res = ''
    var json = JSON.parse(value)

    for (i = 0; i < json.length; i++) {

        if (json[i].name =~ "{HOST.NAME}") {

            res = JSON.stringify(json[i])

            return res
        }

    }

}

{HOST.NAME} 是 SOS-PE02S-PK00400

一切正常,除了字段(名称)是 -1。

结果如下:

{
   "name":-1,
   "ip":"",
   "pk":0.4,
   "roadCode":"A32",
   "roadName":"A32",
   "roadDescription":"A32 TORINO-BARDONECCHIA",
   "directionName":"A32_DIR_A",
   "directionDescription":"BARDONECCHIA",
   "sectionName":"A32_DIR_A_SEZ1",
   "sectionDescription":"RIVOLI - SUSA",
   "laneName":null,
   "laneDescription":null,
   "tunnelName":"A32_GALLERY_LA_PEROSA",
   "tunnelDescription":"LA PEROSA",
   "idTecnico":"2002"
}

这是我期望的结果:

{
   "name":"PE02S-PK00400_2002",
   "ip":"",
   "pk":0.4,
   "roadCode":"A32",
   "roadName":"A32",
   "roadDescription":"A32 TORINO-BARDONECCHIA",
   "directionName":"A32_DIR_A",
   "directionDescription":"BARDONECCHIA",
   "sectionName":"A32_DIR_A_SEZ1",
   "sectionDescription":"RIVOLI - SUSA",
   "laneName":null,
   "laneDescription":null,
   "tunnelName":"A32_GALLERY_LA_PEROSA",
   "tunnelDescription":"LA PEROSA",
   "idTecnico":"2002"
}

我不明白哪里错了。如果你能帮助我,我将不胜感激。 非常感谢。

=用于assignment但不用于比较。

===== 对于 comparison

因为 for 循环完美地开始但是当它进入 if 条件时,首先你没有检查相等性而是你分配给 json[i].name 这是第一个 element/object 的 json 排列到任何 ~"{HOST.NAME}" return 中,这将是 -1(see the below snippet).

然后你 console.log 那个对象,即 json[0] 并且你将 JSON.stringify(json[i]) 的结果分配给 res 和 return 结果。

console.log( ~"AnythingString" );

要检查字符串中是否包含某些文本,您可以使用 includes

const name = "marcombar";

const result = name.includes("bar");
console.log(result);

如果你需要 check/compare 它与 json[i].name

const json = [{
    name: "PE02S-PK00400_2002",
    ip: "",
    pk: 0.4,
    roadCode: "A32",
    roadName: "A32",
    roadDescription: "A32 TORINO-BARDONECCHIA",
    directionName: "A32_DIR_A",
    directionDescription: "BARDONECCHIA",
    sectionName: "A32_DIR_A_SEZ1",
    sectionDescription: "RIVOLI - SUSA",
    laneName: null,
    laneDescription: null,
    tunnelName: "A32_GALLERY_LA_PEROSA",
    tunnelDescription: "LA PEROSA",
    idTecnico: "2002",
  },
  {
    name: "PE04S-PK00500_2004",
    ip: "",
    pk: 0.5,
    roadCode: "A32",
    roadName: "A32",
    roadDescription: "A32 TORINO-BARDONECCHIA",
    directionName: "A32_DIR_A",
    directionDescription: "BARDONECCHIA",
    sectionName: "A32_DIR_A_SEZ1",
    sectionDescription: "RIVOLI - SUSA",
    laneName: null,
    laneDescription: null,
    tunnelName: "A32_GALLERY_LA_PEROSA",
    tunnelDescription: "LA PEROSA",
    idTecnico: "2004",
  },
  {
    name: "PE06S-PK00750_2006",
    ip: "",
    pk: 0.75,
    roadCode: "A32",
    roadName: "A32",
    roadDescription: "A32 TORINO-BARDONECCHIA",
    directionName: "A32_DIR_A",
    directionDescription: "BARDONECCHIA",
    sectionName: "A32_DIR_A_SEZ1",
    sectionDescription: "RIVOLI - SUSA",
    laneName: null,
    laneDescription: null,
    tunnelName: "A32_GALLERY_LA_PEROSA",
    tunnelDescription: "LA PEROSA",
    idTecnico: "2006",
  },
];

function convert() {
  for (i = 0; i < json.length; i++) {
    const nameToCheck = "SOS-PE02S-PK00400";
    if (json[i].name.includes(nameToCheck)) {
      console.log("Equal");
      console.log(json[i]);
      res = JSON.stringify(json[i]);
      return res;
    } else {
      console.log("Not Equal");
    }
  }
}
convert();