格式化 json 数据
有时候,我们在命令行请求 Restful
风格的接口,返回的 json 响应是连在一起,如何格式化 json 数据呢?一般的做法是,先复制 json 数据,然后粘贴到类似 www.bejson.com 在线网站,格式化数据。可以在命令行中,直接格式化 json 数据嘛?当然可以了,使用 python json
包的工具就可以,例如。
➜ ~ curl --silent 'https://www.jianshu.com/users/recommended' | python3 -m json.tool
{
"users": [
{
"id": 3292545,
"slug": "c4165d16d0ad",
"nickname": "\u5317\u7f8e\u4e4b\u5317",
"avatar_source": "http://upload.jianshu.io/users/upload_avatars/3292545/78f2855a-80fd-419a-9fb2-1aeed39690f0.png",
"total_likes_count": 28933,
"total_wordage": 407114,
"is_following_user": false
},
...
{
"id": 1835526,
"slug": "55b597320c4e",
"nickname": "\u884c\u8ddd\u7248\u541b",
"avatar_source": "http://upload.jianshu.io/users/upload_avatars/1835526/83d24e1a-0a0f-43f6-8a1d-289be6101e73.png",
"total_likes_count": 53898,
"total_wordage": 495624,
"is_following_user": false
}
],
"total_count": 21115
}
jq 命令
使用 jq
也可以美化 json 数据, e.g.
➜ ~ curl --silent 'https://www.jianshu.com/users/recommended' | jq '.'
{
"users": [
{
"id": 3292545,
"slug": "c4165d16d0ad",
"nickname": "北美之北",
"avatar_source": "http://upload.jianshu.io/users/upload_avatars/3292545/78f2855a-80fd-419a-9fb2-1aeed39690f0.png",
"total_likes_count": 28933,
"total_wordage": 407114,
"is_following_user": false
},
...
{
"id": 1835526,
"slug": "55b597320c4e",
"nickname": "行距版君",
"avatar_source": "http://upload.jianshu.io/users/upload_avatars/1835526/83d24e1a-0a0f-43f6-8a1d-289be6101e73.png",
"total_likes_count": 53898,
"total_wordage": 495624,
"is_following_user": false
}
],
"total_count": 21146
}
如何在命令行中,提取 json 数据的字段值呢?使用 jq
命令,可以帮助你。
例如请求简书的推荐用户接口 GET https://www.jianshu.com/users/recommended
,过滤出字段 total_count
。
e.g.
➜ ~ curl --silent 'https://www.jianshu.com/users/recommended' | jq '.total_count'
21129
jq
命令不是系统自带的,使用前,需要安装该命令。从jq 官网 下载该命令,然后放到系统 PATH 中。
# Linux
sudo wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O /usr/local/bin/jq
sudo chmod +x /usr/local/bin/jq
# Mac OS X
sudo wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64 -O /usr/local/bin/jq
sudo chmod +x /usr/local/bin/jq
# Windows 下
# 从下载 https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64.exe
# 然后放到系统 PATH 下.
还有一个网站 jqplay 用于在线使用 jq
命令,可以登上去,试下。
基础使用和使用例子如下,点击官网文档查看更多使用。
1. 过滤 key 值, `.foo, .foo.bar, .foo?`
➜ ~ echo '{"foo": 42, "bar": "less interesting data"}' | jq '.foo'
42
2. 数组操作 `.[], .[]?, .[2], .[10:15]`
➜ ~ echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq '.[1]'
{
"name": "XML",
"good": false
}
3. 构造数组/对象 `[], {}`
➜ ~ echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' | jq '{user, title: .titles[]}'
{
"user": "stedolan",
"title": "JQ Primer"
}
{
"user": "stedolan",
"title": "More JQ"
}
4. 获取 json 值的长度 `length`
➜ ~ echo '[[1,2], "string", {"a":2}, null]' | jq '.[] | length'
2
6
1
0
5. 过滤 json 对象的 keys `keys`
➜ ~ echo '{"abc": 1, "abcd": 2, "Foo": 3}' | jq 'keys'
[
"Foo",
"abc",
"abcd"
]
6. 使用 `,` 过滤多个值
➜ ~ echo '{ "foo": 42, "bar": "something else", "baz": true}' | jq '.foo, .bar'
42
"something else"
7. 使用管道符号 `|` 多次过滤
➜ ~ echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq '.[] | .name'
"JSON"
"XML"
8. 如果正确,则选择值 `select(foo)`
➜ ~ echo '[1,5,3,0,7]' | jq 'map(select(. >= 2))'
[
5,
3,
7
]
9. 对每个输入进行操作 `map(foo)`
➜ ~ echo '[1,2,3]' | jq 'map(.+1)'
[
2,
3,
4
]
10. if-then-else-end 条件
➜ ~ echo '2' | jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end'
"many"
11. 字符插入 `\(foo)`
➜ ~ echo '42' | jq '"The input was \(.), which is one less than \(.+1)"'
"The input was 42, which is one less than 43"
网友评论