Jq is a lightweight and flexible command-line JSON processor.
jq 是一个用来处理json数据的命令行工具
brew install jq
jq [options] <jq filter> [file...]
把输入转输出
echo '{"foo": 0}' | jq .
{
"foo": 0
}
jq '.foo'
Input: {"foo": 42, "bar": "less interesting data"}
Output: 42
数组索引从0开始,所以 .[2]
表示第3个元素。
jq '.[0]'
Input: [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
Output: {"name":"JSON", "good":true}
.[10: 15]
表示提取数组内的元素:从第 11 个元素开始到第16个元素结束,包括第 11 个元素,不包括第 16 个元素
jq '.[2:4]'
Input ["a","b","c","d","e"]
Output ["c", "d"]
jq '.[]'
Input [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
Output {"name":"JSON", "good":true}
{"name":"XML", "good":false}
jq '.foo, .bar'
Input {"foo": 42, "bar": "something else", "baz": true}
Output 42
"something else"
jq '.[] | .name'
Input [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
Output "JSON"
"XML"
- 用于数组的个数
- 用于对象的key-value对数
- 字符串的支付长度
- null 的 length 为0
jq '.[] | length'
Input [[1,2], "string", {"a":2}, null]
Output 2
6
1
0
- 用于数组,表示数组的索引。从 0 到 length -1
- 用与对象,表示对象的所有key
jq 'keys'
Input {"abc": 1, "abcd": 2, "Foo": 3}
Output ["Foo", "abc", "abcd"]
- 对数组的每个元素执行 x 操作, 返回一个新数组
jq 'map(.+1)'
Input [1,2,3]
Output [2,3,4]
jq 'map_values(.+1)'
Input {"a": 1, "b": 2, "c": 3}
Output {"a": 2, "b": 3, "c": 4}
- 判断 key 是否存在
jq 'map(has("foo"))'
Input [{"foo": 42}, {}]
Output [true, false]
- 对输入内容进行排序
jq 'sort'
Input [8,3,null,6]
Output [null,3,6,8]
jq 'sort_by(.foo)'
Input [{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]
Output [{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]
jq '[.[]|startswith("foo")]'
Input ["fo", "foo", "barfoo", "foobar", "barfoob"]
Output [false, true, false, true, false]
jq '[.[]|endswith("foo")]'
Input ["foobar", "barfoo"]
Output [false, true]
jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end'
Input 2
Output "many"