jq - 一个灵活轻量的命令行JSON处理器
jq是一个轻量级和灵活的命令行JSON处理器。官方文档 参考链接
安装
$ brew install jq
示例
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀
{"result":[["美图秀秀vip","3193.8094555873927"]]}%
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀 | jq .
{
"result": [
[
"美图秀秀vip",
"4020.6379542395694"
]
]
}
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀 | jq -c
{"result":[["美图秀秀vip","4020.6379542395694"]]}
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀 | jq '.result[][0]'
"美图秀秀vip"
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|jq '.[]' -c
{"name":"lee","age":12}
{"name":"han","age":21}
{"name":"tom","age":50}
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]' | jq '.[] | .name'
"lee"
"han"
"tom"
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]' | jq 'map(select(.age>20))' -c
[{"name":"han","age":21},{"name":"tom","age":50}]
类似指令
node 命令行 json 处理器 官方文档
$ npm install -g json
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀
{"result":[["美图秀秀vip","3193.8094555873927"]]}
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀 | json
{
"result": [
[
"美图秀秀vip",
"3193.8094555873927"
]
]
}
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美图秀秀 | json result | json -a 0
美图秀秀vip
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'| json -a
{
"name": "lee",
"age": 12
}
{
"name": "han",
"age": 21
}
{
"name": "tom",
"age": 50
}
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|json -o jsony-0
[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'| json -c 'this.age > 20'
[
{
"name": "han",
"age": 21
},
{
"name": "tom",
"age": 50
}
]
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]' > sample.json
$ json -f sample.json -a 0 name age
lee 12
han 21
tom 50
$ json -f sample.json -e 'this.gender=1'
[
{
"name": "lee",
"age": 12,
"gender": 1
},
{
"name": "han",
"age": 21,
"gender": 1
},
{
"name": "tom",
"age": 50,
"gender": 1
}
]
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|json 1 -o jsony-0
{"name":"han","age":21}
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|json -- -1
{
"name": "tom",
"age": 50
}
作者:Plucky 出处:https://www.1991.wiki/topics/2 版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可,如您转载必须以链接形式注明原文地址。
|