近期准备对 自用的一个 shell 进行完善,有些增删改应该用 jq 来做应该事半功倍一点,将之前整理的一些资料一并做整理做个记录,方便后续查找学习。

语法命令对照表

语法 json 命令 result str 说明
. “Hello, world!” jq ‘.’ “Hello, world!”
{“foo”: 42, “bar”: “less interesting data”} jq ‘.foo’ 42 返回对象foo key 值
{“notfoo”: true, “alsonotfoo”: false} jq ‘.foo’ null 对象key不存在,返回null
.[] [1,2,3] jq ‘.[]’ 1
2
3
元素遍历
{“a”: 1, “b”: 2} jq ‘.[]’ 1
2
key 对值遍历
.[<string>] {“foo”: 42} jq .[“foo”] 42 获取key 对应值
.[<value>] [{“name”:“JSON”, “good”:true}, {“name”:“XML”, “good”:false}] jq ‘.[0]’ {“name”:“JSON”, “good”:true} 返回1下标元素
[1,2,3] jq ‘.[-2]’ 2 返回倒数两个位置元素
.[start:end] [“a”,“b”,“c”,“d”,“e”] jq ‘.[2:4]’ [“c”, “d”] 下标开始到下标结束的元素
“abcdefghi” jq ‘.[2:4]’ “cd” 下标开始到下标结束的字符
.[:end] [“a”,“b”,“c”,“d”,“e”] jq ‘.[:3]’ [“a”, “b”, “c”] 首个元素到指定下标元素之间的元素
.[start:] [“a”,“b”,“c”,“d”,“e”] jq ‘.[-2:]’ [“d”, “e”] 指定下标开始到未元素之间的元素
, {“foo”: 42, “bar”: “something else”, “baz”: true} jq ‘.foo, .bar’ 42, “something else” 获取多个key值
{“user”:“stedolan”, “projects”: [“jq”, “wikiflow”]} jq ‘.user, .projects[]’ “stedolan”, “jq”, “wikiflow” b取多个元素的值
[“a”,“b”,“c”,“d”,“e”] jq ‘.[4,2]’ “e”, “c” 取4 和 2 下标元素
| [{“name”:“JSON”, “good”:true}, {“name”:“XML”, “good”:false}] jq ‘.[] | .name’ “JSON”, “XML” 管道
[] {“user”:“stedolan”, “projects”: [“jq”, “wikiflow”]} jq ‘[.user, .projects[]]’ [“stedolan”, “jq”, “wikiflow”] 元素值组成新数组
[1, 2, 3] jq ‘[ .[] | . * 2]’ [2, 4, 6] 元素值组成新数组
{} [{“user”:“stedolan”, “title”: “JQ Primer”}] jq ‘.[] | {user, title} {“user”:“stedolan”, “title”: “JQ Primer”} 组成对象(使用原key名)
[{“user”:“stedolan”, “title”: “JQ Primer”}] jq ‘.[] | {user1: .user, title: .title}’ {“user1”:“stedolan”, “title”: “JQ Primer”} 组成对象(改变key名)
+ {“a”: 7} jq ‘.a + 1’ 8 算术运算符
{“a”: [1,2], “b”: [3,4]} jq ‘.a + .b’ [1,2,3,4] 数组元素合并
jq ‘{a: 1} + {b: 2} + {c: 3} + {a: 42}’ {“a”: 42, “b”: 2, “c”: 3} 合并属性
- {“a”:3} jq ‘4 - .a’ 1 算术运算符
[“xml”, “yaml”, “json”] jq ‘. - [“xml”, “yaml”]’ [“json”] 删除指定数组内元素
/ 5 jq '10 / . ’ 2 算术运算符
“a,b,c,d,e” jq ‘. / “,”’ [“a”,“b”,“c”,“d”,“e”] 字符串分隔成数组
length [1,2] jq ‘. | length’ 2 数组元素个数
“string” jq ‘. | length’ 6 string 的长度
{“a”:2} jq ‘. | length’ 1 对象key个数
keys [“a”,“b”,“c”] jq ’ . | keys’ [0,1,2] 数组下标数组
{“abc”: 1, “abcd”: 2, “Foo”: 3} jq ’ . | keys’ [“Foo”, “abc”, “abcd”] kye数组
has(key) {“abc”: 1, “abcd”: 2, “Foo”: 3} jq ‘. | has(“abc”)’ true 是否包含key
map(x) [1,2,3] jq ‘map(.+1)’ [2,3,4] 元素遍历
map_values(x) {“a”: 1, “b”: 2, “c”: 3} jq ‘map_values(.+1)’ {“a”: 2, “b”: 3, “c”: 4} 对象值遍历
del(path_expression) {“foo”: 42, “bar”: 9001, “baz”: 42} jq ‘del(.foo)’ {“bar”: 9001, “baz”: 42} 删除key
[“foo”, “bar”, “baz”] jq ‘del(.[1, 2])’ [“foo”] 删除元素
getpath(PATHS) {“a”:{“b”:0, “c”:1}} jq ‘[getpath([“a”,“b”], [“a”,“c”])]’ [0, 1] 获取指定值
setpath(PATHS; VALUE) {“a”:{“b”:0}} jq ‘setpath([“a”,“b”]; 1)’ {“a”: {“b”: 1}} 设置指定值
jq ‘setpath([0,“a”]; 1)’ [{“a”:1}] 设置指定值
select(boolean_expression) [1,5,3,0,7] jq ‘map(select(. >= 2))’ [5,3,7] select
[{“id”: “first”, “val”: 1}, {“id”: “second”, “val”: 2}] jq ‘.[] | select(.id == “second”)’ {“id”: “second”, “val”: 2} select
arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars [[],{},1,“foo”,null,true,false] jq ‘.[] | numbers’ 1 从数组中取指定类型值
add [“a”,“b”,“c”] jq ‘add’ “abc” 字符串元素拼接
[1, 2, 3] jq ‘add’ 6 sum
any, any(condition), any(generator; condition) [true, false] jq ‘any’ true 有一个元素是true返回true
[false, false] jq ‘any’ false
[] jq ‘any’ false
[1,2,3] jq ‘any(. > 1)’ true
all, all(condition), all(generator; condition) [true, false] jq ‘all’ false 所有元素是true返回true
range(upto), range(from;upto) range(from;upto;by) jq ‘[range(4)]’ [0,1,2,3] 步进
jq ‘range(2;4)’ 2, 3
jq ‘[range(2;4)]’ [2,3]
jq ‘[range(0;10;3)]’ [0,3,6,9]
jq ‘[range(0;-5;-1)]’ [0,-1,-2,-3,-4]
floor 3.14159 jq ‘floor’ 3 取整
sqrt 9 jq ‘sqrt’ 3 开根
tonumber [1, “1”] jq ‘.[] | tonumber’ 1, 1 转数字
tostring [1, “1”, [1]] jq ‘.[] | tostring’ [“1”, “1”, “[1]”] 转json 字符串
type [0, false, [], {}, null, “hello”] jq ‘map(type)’ [“number”, “boolean”, “array”, “object”, “null”, “string”] 查看类型
sort, sort_by(path_expression) [8,3,null,6] jq ‘sort’ [null,3,6,8] order by
[{“foo”:4, “bar”:10}, {“foo”:3, “bar”:100}, {“foo”:2, “bar”:1}] jq ‘sort_by(.foo)’ [{“foo”:2, “bar”:1}, {“foo”:3, “bar”:100}, {“foo”:4, “bar”:10}]
group_by(path_expression) [{“foo”:1, “bar”:10}, {“foo”:3, “bar”:100}, {“foo”:1, “bar”:1}] jq ‘group_by(.foo)’ [[{“foo”:1, “bar”:10}, {“foo”:1, “bar”:1}], [{“foo”:3, “bar”:100}]] 聚合函数group by
min, max, min_by(path_exp), max_by(path_exp)
unique, unique_by(path_exp)
reverse
contains(element) “foobar” jq ‘contains(“bar”)’ true
[“foobar”, “foobaz”, “blarp”] jq ‘contains([“baz”, “bar”])’ true
[“foobar”, “foobaz”, “blarp”] jq ‘contains([“bazzzzz”, “bar”])’ false
{“foo”: 12, “bar”:[1,2,{“barp”:12, “blip”:13}]} jq ‘contains({foo: 12, bar: [{barp: 12}]})’ true
{“foo”: 12, “bar”:[1,2,{“barp”:12, “blip”:13}]} jq ‘contains({foo: 12, bar: [{barp: 15}]})’ false
indices(s) “a,b, cd, efg, hijk” jq ‘indices(", ")’ [3,7,12] 返回出现元素下标位置集合
[0,1,2,1,3,1,4] jq ‘indices(1)’ [1,3,5]
[0,1,2,3,1,4,2,5,1,2,6,7] jq ‘indices([1,2])’ [1,8] 参数s可以输入为数组
index(s), rindex(s) “a,b, cd, efg, hijk” jq ‘index(", ")’ 3 indexof
“a,b, cd, efg, hijk” jq 'rindex(", ") 12 lastIndexOf
inside “bar” jq ‘inside(“foobar”)’ true 包含元素
{“foo”: 12, “bar”: [{“barp”: 15}]} jq ‘inside({“foo”: 12, “bar”:[1,2,{“barp”:12, “blip”:13}]})’ false
startswith(str) [“fo”, “foo”, “barfoo”, “foobar”, “barfoob”] jq ‘[.[] | startswith(“foo”)]’ [false, true, false, true, false] 开始于
endswith(str) 结束于
combinations, combinations(n) [[1,2], [3, 4]] jq ‘combinations’ [1, 3], [1, 4], [2, 3], [2, 4] 组合_笛卡尔积_
[0, 1] jq ‘combinations(2)’ [0, 0], [0, 1], [1, 0], [1, 1]
ltrimstr(str)
rtrimstr(str) [“fo”, “foo”, “barfoo”, “foobar”, “afoo”] jq ‘[.[] | ltrimstr(“foo”)]’ [“fo”,“”,“barfoo”,“bar”,“afoo”]
split(str) split
join(str) [“a”,1,2.3,true,null,false] jq ‘join(" ")’ “a 1 2.3 true false” 转string指定分隔符
while(cond; update)
until(cond; next) 4 jq ‘[.,1] until(.[0] < 1; [.[0] - 1, .[1] * .[0]]) | .[1]’ 24 (4*3*2*1)
foreach
tojson
Format strings and escaping ### ###
@text ### ###
@json ### ###
@html ### ###
@uri ### ###
@csv ### ###
@tsv ### ###
@sh ### ###
@base64 “This is a message” jq '@base64 “VGhpcyBpcyBhIG1lc3NhZ2U=” 加密
@base64d ### ### 解密
==, != >, >=, <=, < and/or/not
+=, -=, *=, /=, %=, //=
if A then elif B else C end
// {} jq ‘.foo // 42’ 42 提供默认值
isempty(exp)
limit(n; exp) [0,1,2,3,4,5,6,7,8,9] jq ‘[limit(3;.[])]’ [0,1,2]
first(expr), last(expr), nth(n; expr) 10 jq ‘[first(range(.)), last(range(.)), nth(./2; range(.))]’ [0,9,5]
first, last, nth(n) 10 jq ‘[range(.)] | [first, last, nth(5)]’ [0,9,5]
? 忽略异常

参考链接

  1. 常用 linux jq 命令语法整理
  2. linux shell jq命令 合并json文件
  3. jq append json array
  4. jq 常用操作
  5. 使用 Shell 脚本来处理 JSON
  6. jq简易教程
  7. jq基本用法:读取访问
  8. jq中文手册
  9. A playground for jq