计算嵌套元素的JSON数据中的出现次数

时间:2017-03-29 12:55:42

标签: javascript arrays json node.js

如何在嵌套元素的JSON数据中获取元素的外观?

[
    {
            "client":"172.16.109.96",
            "check":
                    {
                            "command":"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'highestCurrent(scale(vcenter.*.*.*.*.cpu.usage_percent_average, 0.01), 5)'  -w 1 -c 6 -p -6hours",
                            "standalone":true,
                            "interval":5,
                            "name":"vm_cpu_average",
                            "issued":1490774610,
                            "executed":1490774610,
                            "duration":0.142,
                            "output":"CheckGraphiteStat WARNING: No data from Graphite\n",
                            "status":3,
                            "type":"standard"
                    }
    },
    {
            "client":"172.16.109.96",
            "check":
                    {
                            "command":"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'collectd.*.*.system.memory.used.pct' -w 50 -c 75 -p -6hours",
                            "standalone":true,
                            "interval":5,
                            "name":"mem_average",
                            "issued":1490774614,
                            "executed":1490774614,
                            "duration":0.223,
                            "output":"CheckGraphiteStat CRITICAL: collectd.DEMO.172_16_109_100.system.memory.used.pct is 94.44555555555556 \n",
                            "status":2,
                            "type":"standard"
                    }
    },
    {
            "client":"172.16.109.96",
            "check":
                    {
                            "command":"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'scale(vcenter.*.*.*.*.mem.usage_percent_average, 0.01)' -w 50 -c 75 -p -6hours",
                            "standalone":true,
                            "interval":5,
                            "name":"vm_mem_average",
                            "issued":1490774614,
                            "executed":1490774614,
                            "duration":0.216,
                            "output":"CheckGraphiteStat WARNING: No data from Graphite\n",
                            "status":3,
                            "type":"standard"
                    }
    },
    {
            "client":"172.16.109.96",
            "check":
                    {
                            "command":"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'absolute(offset(collectd.*.*.system.cpu.idle.pct, -100))'  -c 7 -p -6hours",
                            "standalone":true,
                            "interval":5,
                            "name":"cpu_average",
                            "issued":1490774613,
                            "executed":1490774613,
                            "duration":0.124,
                            "output":"CheckGraphiteStat CRITICAL: absolute(offset(collectd.DEMO.172_16_109_100.system.cpu.idle.pct,-100)) is 10.585147058823525 \n",
                            "status":2,
                            "type":"standard"
                    }
    }

我需要计算WARNING和CRITICAL的出现次数,如果它出现在输出(检查 - >输出)中。

请原谅这是否是一个新手问题。我是新手。

4 个答案:

答案 0 :(得分:2)

你需要计算前端的回复吗?如果您愿意,可以使用正则表达式解析json。

var numMatches = (JSON.stringify(yourData).match(/(WARNING|CRITICAL)/g) || []).length;

这应该比在每个项目上迭代更快,并且分别在每个属性上运行单独的正则表达式。因此,如果您要在大型数据集上运行,我建议使用其他解决方案。

答案 1 :(得分:2)

您可以执行以下操作:

let critialCount = 0
let warningCount = 0

for(let i = 0; i < data.length; i++) {
  if(data[i].check.output.indexOf('CRITICAL') > -1) {
    critialCount++
  }
  else if(data[i].check.output.indexOf('WARNING') > -1) {
    warningCount++
  }
}

console.log('Critical count: ' + critialCount)
console.log('Warning count: ' + warningCount)

工作示例:

const data =[
    {
        "client": "172.16.109.96",
        "check": {
            "command": "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'highestCurrent(scale(vcenter.*.*.*.*.cpu.usage_percent_average, 0.01), 5)'  -w 1 -c 6 -p -6hours",
            "standalone": true,
            "interval": 5,
            "name": "vm_cpu_average",
            "issued": 1490774610,
            "executed": 1490774610,
            "duration": 0.142,
            "output": "CheckGraphiteStat WARNING: No data from Graphite\n",
            "status": 3,
            "type": "standard"
        }
    },
    {
        "client": "172.16.109.96",
        "check": {
            "command": "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'collectd.*.*.system.memory.used.pct' -w 50 -c 75 -p -6hours",
            "standalone": true,
            "interval": 5,
            "name": "mem_average",
            "issued": 1490774614,
            "executed": 1490774614,
            "duration": 0.223,
            "output": "CheckGraphiteStat CRITICAL: collectd.DEMO.172_16_109_100.system.memory.used.pct is 94.44555555555556 \n",
            "status": 2,
            "type": "standard"
        }
    },
    {
        "client": "172.16.109.96",
        "check": {
            "command": "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'scale(vcenter.*.*.*.*.mem.usage_percent_average, 0.01)' -w 50 -c 75 -p -6hours",
            "standalone": true,
            "interval": 5,
            "name": "vm_mem_average",
            "issued": 1490774614,
            "executed": 1490774614,
            "duration": 0.216,
            "output": "CheckGraphiteStat WARNING: No data from Graphite\n",
            "status": 3,
            "type": "standard"
        }
    },
    {
        "client": "172.16.109.96",
        "check": {
            "command": "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'absolute(offset(collectd.*.*.system.cpu.idle.pct, -100))'  -c 7 -p -6hours",
            "standalone": true,
            "interval": 5,
            "name": "cpu_average",
            "issued": 1490774613,
            "executed": 1490774613,
            "duration": 0.124,
            "output": "CheckGraphiteStat CRITICAL: absolute(offset(collectd.DEMO.172_16_109_100.system.cpu.idle.pct,-100)) is 10.585147058823525 \n",
            "status": 2,
            "type": "standard"
        }
    }
]

let critialCount = 0
let warningCount = 0

for(let i = 0; i < data.length; i++) {
  if(data[i].check.output.indexOf('CRITICAL') > -1) {
    critialCount++
  }
  else if(data[i].check.output.indexOf('WARNING') > -1) {
    warningCount++
  }
}

console.log('Critical count: ' + critialCount)
console.log('Warning count: ' + warningCount)

答案 2 :(得分:1)

您可以使用Array#forEach来迭代每个output条目,并查找warningcritical个关键字。

var json = [{client:"172.16.109.96",check:{command:"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'highestCurrent(scale(vcenter.*.*.*.*.cpu.usage_percent_average, 0.01), 5)'  -w 1 -c 6 -p -6hours",standalone:!0,interval:5,name:"vm_cpu_average",issued:1490774610,executed:1490774610,duration:.142,output:"CheckGraphiteStat WARNING: No data from Graphite\n",status:3,type:"standard"}},{client:"172.16.109.96",check:{command:"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'collectd.*.*.system.memory.used.pct' -w 50 -c 75 -p -6hours",standalone:!0,interval:5,name:"mem_average",issued:1490774614,executed:1490774614,duration:.223,output:"CheckGraphiteStat CRITICAL: collectd.DEMO.172_16_109_100.system.memory.used.pct is 94.44555555555556 \n",status:2,type:"standard"}},{client:"172.16.109.96",check:{command:"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'scale(vcenter.*.*.*.*.mem.usage_percent_average, 0.01)' -w 50 -c 75 -p -6hours",standalone:!0,interval:5,name:"vm_mem_average",issued:1490774614,executed:1490774614,duration:.216,output:"CheckGraphiteStat WARNING: No data from Graphite\n",status:3,type:"standard"}},{client:"172.16.109.96",check:{command:"/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'absolute(offset(collectd.*.*.system.cpu.idle.pct, -100))'  -c 7 -p -6hours",standalone:!0,interval:5,name:"cpu_average",issued:1490774613,executed:1490774613,duration:.124,output:"CheckGraphiteStat CRITICAL: absolute(offset(collectd.DEMO.172_16_109_100.system.cpu.idle.pct,-100)) is 10.585147058823525 \n",status:2,type:"standard"}}],
    obj = {};
    json.forEach(function(v){
      var elem = v.check.output.match(/critical|warning/i)[0];
      !obj[elem] ? obj[elem] = 1 : obj[elem]++;
    });

    console.log(obj);

答案 3 :(得分:1)

您可以迭代数组并使用正则表达式检查所需的字符串。然后相应计算。

var data = [{ client: "172.16.109.96", check: { command: "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'highestCurrent(scale(vcenter.*.*.*.*.cpu.usage_percent_average, 0.01), 5)'  -w 1 -c 6 -p -6hours", standalone: true, interval: 5, name: "vm_cpu_average", issued: 1490774610, executed: 1490774610, duration: 0.142, output: "CheckGraphiteStat WARNING: No data from Graphite\n", status: 3, type: "standard" } }, { client: "172.16.109.96", check: { command: "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'collectd.*.*.system.memory.used.pct' -w 50 -c 75 -p -6hours", standalone: true, interval: 5, name: "mem_average", issued: 1490774614, executed: 1490774614, duration: 0.223, output: "CheckGraphiteStat CRITICAL: collectd.DEMO.172_16_109_100.system.memory.used.pct is 94.44555555555556 \n", status: 2, type: "standard" } }, { client: "172.16.109.96", check: { command: "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'scale(vcenter.*.*.*.*.mem.usage_percent_average, 0.01)' -w 50 -c 75 -p -6hours", standalone: true, interval: 5, name: "vm_mem_average", issued: 1490774614, executed: 1490774614, duration: 0.216, output: "CheckGraphiteStat WARNING: No data from Graphite\n", status: 3, type: "standard" } }, { client: "172.16.109.96", check: { command: "/var/lib/gems/1.9.1/gems/sensu-plugins-graphite-1.1.0/bin/check-graphite-stats.rb -h 172.16.109.96:8081 -t 'absolute(offset(collectd.*.*.system.cpu.idle.pct, -100))'  -c 7 -p -6hours", standalone: true, interval: 5, name: "cpu_average", issued: 1490774613, executed: 1490774613, duration: 0.124, output: "CheckGraphiteStat CRITICAL: absolute(offset(collectd.DEMO.172_16_109_100.system.cpu.idle.pct,-100)) is 10.585147058823525 \n", status: 2, type: "standard" } }],
    count = {};

data.forEach(function (a) {
    var match = a.check.output.match(/CRITICAL|WARNING/);
    if (match) {
        count[match[0]] = (count[match[0]] || 0) + 1;
    }
});

console.log(count);