linux中jq读取json文件上传,Linux系统下使用jq工具处理json

1. 简介

json是一种轻量级的数据交换格式,应用范围非常广泛。在Linux系统下使用jq工具可以非常方便的处理json,官方给的定义是:

A jq program is a “filter”: it takes an input, and produces an output. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks.

通俗的说就是一个能够接受json,处理json,输出json的程序,反正很好用。

安装起来也非常的方便,直接使用yum即可安装。

[root@test-dhcp ~]# yum install jq

2. 基本操作

创建

[root@test-dhcp ~]# jq -n {a:1}

{

"a": 1

}

[root@test-dhcp ~]# jq -n '{a:"test"}'

{

"a": "test"

}

合并

[root@test-dhcp ~]# jq -n '{a:"test"} + {b:2}'

{

"a": "test",

"b": 2

}

[root@test-dhcp ~]# jq -n '{a:"test"} + {b:2} + {c:"testc"}'

{

"a": "test",

"b": 2,

"c": "testc"

}

删除

[root@test-dhcp ~]# cat test.json

{"a": "test","b": 2, "c": "testc"}

[root@test-dhcp ~]# cat test.json |jq .

{

"a": "test",

"b": 2,

"c": "testc"

}

[root@test-dhcp ~]# cat test.json |jq 'del(.b)'

{

"a": "test",

"c": "testc"

}

更新

[root@test-dhcp ~]# cat test.json

{"a": "test","b": 2, "c": "testc"}

[root@test-dhcp ~]# cat test.json |jq '.b="testb"'

{

"a": "test",

"b": "testb",

"c": "testc"

}

[root@test-dhcp ~]# cat test.json |jq '. + {d:4}'

{

"a": "test",

"b": 2,

"c": "testc",

"d": 4

}

[root@test-dhcp ~]# cat test.json |jq '. + {d:4}' |jq '.d={dd:5}'

{

"a": "test",

"b": 2,

"c": "testc",

"d": {

"dd": 5

}

}

查询

[root@test-dhcp ~]# cat test.json |jq .

{

"a": "test",

"b": 2,

"c": "testc",

"d": {

"dd": 5

}

}

[root@test-dhcp ~]# cat test.json |jq '. + {d:4}' |jq '.d={dd:5}' |jq .d.dd

5

[root@test-dhcp ~]# echo '{"a":1,"b":2}' |jq '[.a,.b]'

[

1,

2

]

查看数据类型

[root@test-dhcp ~]# echo "{}" |jq -r type

object

[root@test-dhcp ~]# echo '[0, false, [], {}, null, "hello"]' |jq 'map(type)'

[

"number",

"boolean",

"array",

"object",

"null",

"string"

]

查询数组中的值

[root@test-dhcp ~]# echo [1,2,3] |jq .[1]

2

[root@test-dhcp ~]# echo [1,2,3] |jq .[2]

3

查询数组长度

[root@test-dhcp ~]# echo [1,2,3,9] |jq '.|length'

4

[root@test-dhcp ~]# echo [1,2,3] |jq '.|length'

3

数组相加

[root@test-dhcp ~]# echo [1,2,3] |jq '. + [4,5,6]'

[

1,

2,

3,

4,

5,

6

]

高级查询

[root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. >= 2))'

[

2,

3

]

[root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. == 2))'

[

2

]

[root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. != 2))'

[

1,

3

]

[root@test-dhcp ~]# cat test.json

[

{

"id": "0",

"model": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz"

},

{

"id": "1",

"model": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz"

}

]

[root@test-dhcp ~]# cat test.json |jq .[].model

"Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz"

"Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz"

类型转换

[root@test-dhcp ~]# echo '["a","b,c,d","e"]' |jq 'join(",")'

"a,b,c,d,e"

[root@test-dhcp ~]# echo '["a","b,c,d","e",1]' |jq 'join(",")'

jq: error (at :1): string (",") and number (1) cannot be added

[root@test-dhcp ~]# cat test.json

liuxin,30,male

jiaweiqiang,29,femal

[root@test-dhcp ~]# jq -R 'split(",")|{"name":.[0],"age":.[1],"sex":.[2]}' ./test.json

{

"name": "liuxin",

"age": "30",

"sex": "male"

}

{

"name": "jiaweiqiang",

"age": "29",

"sex": "femal"

}

[root@test-dhcp ~]# cat test.json

{

"name": "liuxin",

"age": "30",

"sex": "male"

}

{

"name": "jiaweiqiang",

"age": "29",

"sex": "femal"

}

[root@test-dhcp ~]# cat test.json |jq . -c

{"name":"liuxin","age":"30","sex":"male"}

{"name":"jiaweiqiang","age":"29","sex":"femal"}

3. 总结

我之前写过一个抓取服务器硬件信息的shell脚本,在处理这些抓取到的数据时,使用jq工具很方便。总体来看这个工具还是很不错的。

更全面,更详细的介绍可登录官网学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值