Linux| jq命令对JSON格式数据操作

news/2025/2/12 3:35:33/

演示内容,输出到json1文件中:

cat >  json1 << EOF
{"tokens":[{"token":"网络","start_offset":0,"end_offset":2,"type":"CN_WORD","position":0},{"token":"不是","start_offset":2,"end_offset":4,"type":"CN_WORD","position":1},{"token":"法外","start_offset":4,"end_offset":6,"type":"CN_WORD","position":2},{"token":"之地","start_offset":6,"end_offset":8,"type":"CN_WORD","position":3}]}
EOF

1、获取整个JSON对象

cat json1 |jq .

返回内容:

{"tokens": [{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0},{"token": "不是","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 1},{"token": "法外","start_offset": 4,"end_offset": 6,"type": "CN_WORD","position": 2},{"token": "之地","start_offset": 6,"end_offset": 8,"type": "CN_WORD","position": 3}]
}

2、获取所有数组

cat json1 |jq '.[]'

返回内容:

[{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0},{"token": "不是","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 1},{"token": "法外","start_offset": 4,"end_offset": 6,"type": "CN_WORD","position": 2},{"token": "之地","start_offset": 6,"end_offset": 8,"type": "CN_WORD","position": 3}
]

3、获取tokens数组中第1个索引数据

cat json1 |jq '.tokens[0]'

返回内容:

{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0
}

4、获取tokens数组中第1、2个索引数据

cat json1 |jq '.tokens[0:2]'

返回内容:

[{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0},{"token": "不是","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 1}
]

5、获取tokens数组中第1个索引数据中token的值

cat json1 |jq '.tokens[0].token'

返回内容:

"网络"

6、获取tokens数组中第1个索引数据中tokentype的值

cat json1 |jq '.tokens[0].token,.tokens[0].type'

可以简写成:

cat json1 |jq '.tokens[0]|.token,.type'

返回内容:

"网络"
"CN_WORD"

7、获取tokens数组中所有token的值

cat json1 |jq '.tokens[].token'

返回内容:

"网络"
"不是"
"法外"
"之地"

8、迭代数组,使用map函数对数组中的每个元素执行相同的操作

cat json1 |jq '.[]|map(.token)'

返回内容:

["网络","不是","法外","之地"
]

9、添加过滤条件:获取tokens数组中token == “网络” 的数据

cat json1 |jq '.tokens[]|select(.token == "网络")'

返回内容:

{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0
}

10、添加多个过滤条件:获取tokens数组中token == "网络" and type == "CN_WORD" 的数据

cat json1 |jq '.tokens[]|select(.token == "网络" and .type == "CN_WORD")'

返回内容:

{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0
}

11、添加过滤条件:获取tokens数组中token == "网络" or token == "法外" 的数据

cat json1 |jq '.tokens[]|select(.token == "网络" or .token == "法外")'

返回内容:

{"token": "网络","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0
}
{"token": "法外","start_offset": 4,"end_offset": 6,"type": "CN_WORD","position": 2
}

12、获取所有数组长度 length函数

cat json1 |jq '.[]|length'

返回内容:

4

13、修改tokens数组中所有token值改为TEST

cat json1 |jq '.tokens|map(.token="TEST")'

返回内容:

[{"token": "TEST","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0},{"token": "TEST","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 1},{"token": "TEST","start_offset": 4,"end_offset": 6,"type": "CN_WORD","position": 2},{"token": "TEST","start_offset": 6,"end_offset": 8,"type": "CN_WORD","position": 3}
]

14、转换成字符串,tostring函数

cat json1 | jq '.tokens|tostring'

返回内容:

"[{\"token\":\"网络\",\"start_offset\":0,\"end_offset\":2,\"type\":\"CN_WORD\",\"position\":0},{\"token\":\"不是\",\"start_offset\":2,\"end_offset\":4,\"type\":\"CN_WORD\",\"position\":1},{\"token\":\"法外\",\"start_offset\":4,\"end_offset\":6,\"type\":\"CN_WORD\",\"position\":2},{\"token\":\"之地\",\"start_offset\":6,\"end_offset\":8,\"type\":\"CN_WORD\",\"position\":3}]"

15、if-then-else条件判断语句
判断token值是否=网络,如果满足输出value = 网络,不满足输出value != 网络

cat json1 |jq '.tokens[]|if .token== "网络" then "value = 网络" else "value != 网络" end'

返回内容

"value = 网络"
"value != 网络"
"value != 网络"
"value != 网络"

http://www.ppmy.cn/news/1122166.html

相关文章

MATLAB算法实战应用案例精讲-【概念篇】用户画像(补充篇)

目录 前言 几个相关概念 算法原理 用户画像是什么? 用户画像种类

若依前后端分离如何解决匿名注解启动报错?

SpringBoot2.6.0默认是ant_path_matcher解析方式,但是2.6.0之后默认是path_pattern_parser解析方式。 所以导致读取注解类方法需要对应的调整,当前若依项目默认版本是2.5.x,如果使用大于2.6.x,需要将info.getPatternsCondition().getPatterns()修改为info.getPathPatterns…

linux 安装 wordpress

文章目录 linux 安装 wordpress1. wordpress 简介2. wordpress功能和特点3. 部署要求4. 环境搭建4.1 部署 nginx4.1.1 新增配置文件 4.2 部署 PHP74.2.1 查看当前版本4.2.2 YUM 安装 PHP74.2.3 查看 PHP 版本4.2.4 启动PHP-FPM4.2.5 修改配置文件4.2.6 重启服务 4.3 部署 mysql…

【深度学习】图像去噪(2)——常见网络学习

【深度学习】图像去噪 是在 【深度学习】计算机视觉 系列文章的基础上&#xff0c;再次针对深度学习&#xff08;尤其是图像去噪方面&#xff09;的基础知识有更深入学习和巩固。 1 DnCNN 1.1 网络结构 1.1.1 残差学习 1.1.2 Batch Normalization (BN) 1.1.2.1 背景和目标…

C++之指向引用的指针和指向指针的引用总结(二百三十四)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

C++ 学习系列 -- std::vector (未完待续)

一 std::vector 是什么&#xff1f; vector 是c 中一种序列式容器&#xff0c;与前面说的 array 类似&#xff0c;其内存分配是连续的&#xff0c;但是与 array 不同的地方在于&#xff0c;vector 在运行时是可以动态扩容的&#xff0c;此外 vector 提供了许多方便的操作&…

C/C++统计满足条件的4位数个数 2023年5月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析

目录 C/C统计满足条件的4位数个数 一、题目要求 1、编程实现 2、输入输出 二、解题思路 1、案例分析 三、程序代码 四、程序说明 五、运行结果 六、考点分析 C/C统计满足条件的4位数个数 2019年12月 C/C编程等级考试一级编程题 一、题目要求 1、编程实现 给定若干…

Grafana离线安装部署以及插件安装

Grafana是一个可视化面板&#xff08;Dashboard&#xff09;&#xff0c;有着非常漂亮的图表和布局展示&#xff0c;功能齐全的度量仪表盘和图形编辑器&#xff0c;支持Graphite、zabbix、InfluxDB、Prometheus和OpenTSDB作为数据源。Grafana主要特性&#xff1a;灵活丰富的图形…