linux 使用jq对json数据进行操作

背景: 通过jmeter生成的resultReport报告,在linux上需要获取到报告结果数据。

数据源:jmeter生成的结果数据都保存在resultReport/content/js/dashboard.js中,具体在如下的createTable($("#statisticsTable")开头的这一行。

目标:获取到从{"supportsControllersDiscrimination" 到 "isController": false}]} 这一串json数据,然后通过jq这个工具获取任何想要的值。

jq用法可以参考:https://www.jianshu.com/p/3522fe70de19

下载地址:https://stedolan.github.io/jq/download/


createTable($("#statisticsTable"), {"supportsControllersDiscrimination": true, "overall": {"data": ["Total", 2190, 250, 11.415525114155251, 270.53881278538773, 2001.0, 2002.0, 2004.0, 36.57497870634801, 9.282049033852731, 17.770805150558644, 10, 2006], "isController": false}, "titles": ["Label", "#Samples", "KO", "Error %", "Average response time", "90th pct", "95th pct", "99th pct", "Throughput", "Received KB/sec", "Sent KB/sec", "Min", "Max"], "items": [{"data": ["rbac_right_check", 2180, 250, 11.46788990825688, 271.5954128440369, 2001.0, 2002.0, 2004.0, 36.5324351046537, 9.206588668660869, 17.76675066613041, 10, 2006], "isController": false}, {"data": ["rbac_policy_list", 10, 0, 0.0, 40.2, 142.0, 142.0, 142.0, 9.950248756218905, 6.364661069651742, 3.8479477611940305, 11, 142], "isController": false}]}, function(index, item){

文件中数据行

命令行如下:

cat ./resultReport/content/js/dashboard.js | awk '/statisticsTable/' | awk -F"statisticsTable\")," '{print $2}' | awk -F", function" '{print $1}' | jq-linux64 -r '( .items[0].data[8] | tostring )'


1- cat ./resultReport/content/js/dashboard.js | awk '/statisticsTable/'

获取statisticsTable这一行

2- cat ./resultReport/content/js/dashboard.js | awk '/statisticsTable/' | awk -F"statisticsTable\")," '{print $2}'

以statisticsTable"),作为分隔符,获取第二段,即{"supportsControllersDiscrimination"到最后的一串。

3- cat ./resultReport/content/js/dashboard.js | awk '/statisticsTable/' | awk -F"statisticsTable\")," '{print $2}' | awk -F", function" '{print $1}'

再次以, function作为分隔符,获取第一段,即拿到了从{"supportsControllersDiscrimination" 到 "isController": false}]} 这一串json数据

4- cat ./resultReport/content/js/dashboard.js | awk '/statisticsTable/' | awk -F"statisticsTable\")," '{print $2}' | awk -F", function" '{print $1}' | jq-linux64 -r '( .items[0].data[8] | tostring )'

使用jq 工具,获取 items 下的data的第9个value,即对应的 Throughput

jq工具可以对json数据进行各种操作,使用起来非常方便。

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券