标签:city haima linux jq json 使用 address name
安装:
yum install jq -y
文档:
https://stedolan.github.io/jq/manual/
数据源
333.json
[{
"name": "站长工具",
"url": "http://tool.chinaz.com",
"address": {
"city": "厦门",
"country": "中国"
},
"arrayBrowser": [{
"name": "Google",
"url": "http://www.google.com"
},
{
"name": "Baidu",
"url": "http://www.baidu.com"
}
]
},
{
"name": "站长之家",
"url": "http://tool.zzhome.com",
"address": {
"city": "大连",
"country": "中国"
},
"arrayBrowser": [{
"name": "360",
"url": "http://www.so.com"
},
{
"name": "bing",
"url": "http://www.bing.com"
}
],
"127.0.0.1": {
"error": 200,
"msg": "ok"
}
}
]
例子基础
haima@haima-PC:~/Desktop$ cat 333.json |jq '.'
[
{
"name": "站长工具",
"url": "http://tool.chinaz.com",
"address": {
"city": "厦门",
"country": "中国"
},
"arrayBrowser": [
{
"name": "Google",
"url": "http://www.google.com"
},
{
"name": "Baidu",
"url": "http://www.baidu.com"
}
]
},
{
"name": "站长之家",
"url": "http://tool.zzhome.com",
"address": {
"city": "大连",
"country": "中国"
},
"arrayBrowser": [
{
"name": "360",
"url": "http://www.so.com"
},
{
"name": "bing",
"url": "http://www.bing.com"
}
],
"127.0.0.1": {
"error": 200,
"msg": "ok"
}
}
]
管道线 |
jq支持管道线 |,它如同linux命令中的管道线——把前面命令的输出当作是后面命令的输入。如下命令把.[0]作为{…}的输入,进而访问嵌套的属性,如.name和.address.city。
观察如下几个命令,通过改变|前后的输入和输出来达到不同的效果:
cat json.txt | jq '.[0] | {name:.name,city:.address.city}'
{
"name": "站长工具",
"city": "厦门"
}
cat json.txt | jq '.[0] | {name:.arrayBrowser[1].name,city:.address.city}'
{
"name": "Baidu",
"city": "厦门"
}
cat json.txt | jq ".[] | {name:.arrayBrowser[1].name,city:.address.city}"
{
"name": "Baidu",
"city": "厦门"
}
{
"name": "bing",
"city": "大连"
}
[]
如果希望把jq的输出当作一个数组,可以在前后加上[]:
cat json.txt | jq "[.[] | {name:.arrayBrowser[1].name,city:.address.city}]"
[
{
"name": "Baidu",
"city": "厦门"
},
{
"name": "bing",
"city": "大连"
}
]
自定义key
在{}中,冒号前面的名字是映射的名称,你可以任意修改,如:
```sh
cat json.txt | jq "[.[] | {name_001:.arrayBrowser[1].name,city_002:.address.city}]"
[
{
"name_001": "Baidu",
"city_002": "厦门"
},
{
"name_001": "bing",
"city_002": "大连"
}
]
原文链接:https://blog.csdn.net/qq_26502245/article/details/100191694
取0号单元下的元素
haima@haima-PC:~/Desktop$ cat 333.json |jq '.[0].name'
"站长工具"
haima@haima-PC:~/Desktop$ cat 333.json |jq '.[0].address'
{
"city": "厦门",
"country": "中国"
}
haima@haima-PC:~/Desktop$ cat 333.json |jq '.[0].address.city'
"厦门"
haima@haima-PC:~/Desktop$ cat 333.json |jq '.[0].address["city"]'
"厦门"
haima@haima-PC:~/Desktop$ cat 333.json |jq ".[0].address[\"city\"]"
"厦门"
haima@haima-PC:~/Desktop$ cat 333.json |jq '.[1]["127.0.0.1"]'
{
"error": 200,
"msg": "ok"
}
haima@haima-PC:~/Desktop$ cat 333.json | jq ".[0]" | jq 'keys' #获取0号单元的所有key
[
"address",
"arrayBrowser",
"name",
"url"
]
haima@haima-PC:~/Desktop$ cat 333.json | jq ".[0].address" # 获取0号单元的address
{
"city": "厦门",
"country": "中国"
}
haima@haima-PC:~/Desktop$ cat 333.json | jq ".[0].address" | jq 'keys' #获取0号单元的address里的所有 key(嵌套提取)
[
"city",
"country"
]
haima@haima-PC:~/Desktop$ cat 333.json | jq ".[0]" | jq 'has("name")' #判断是否有某个 key
true
haima@haima-PC:~/Desktop$ cat 333.json | jq ".[0]" | jq 'has("test")' #判断是否有某个 key
false
压缩json
haima@haima-PC:~/Desktop$ cat 333.json | jq -c .
[{"name":"站长工具","url":"http://tool.chinaz.com","address":{"city":"厦门","country":"中国"},"arrayBrowser":[{"name":"Google","url":"http://www.google.com"},{"name":"Baidu","url":"http://www.baidu.com"}]},{"name":"站长之家","url":"http://tool.zzhome.com","address":{"city":"大连","country":"中国"},"arrayBrowser":[{"name":"360","url":"http://www.so.com"},{"name":"bing","url":"http://www.bing.com"}],"127.0.0.1":{"error":200,"msg":"ok"}}]
标签:city,haima,linux,jq,json,使用,address,name 来源: https://www.cnblogs.com/haima/p/15135587.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。