简体   繁体   中英

extract json with javascript preprocessing Zabbix

i'm trying to extrapolate a json if a string is contained. My json is this:

[
   {
      "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"
   }
]

The javascript script is this:

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} is SOS-PE02S-PK00400

Everything works, except the field (name) is -1.

Here is the result:

{
   "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"
}

Here is the result I would expect:

{
   "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"
}

I don't understand where is wrong. If you can help me I am grateful. Thank you very much.

= is used for assignment but not for comparison.

== or === for comparison .

Since for loop starts perfectly but when it goes to if condition, first of all you're not checking for equality instead you are assigining to json[i].name which is the first element/object of json arrray to whatever ~"{HOST.NAME}" returns which would be -1(see the below snippet) .

Then you console.log that object ie json[0] and you're assigning the result of JSON.stringify(json[i]) to res and return the result.

 console.log( ~"AnythingString" );

To check if some text includes in a string then you can use includes

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

If you need to check/compare it with 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM