linux jq 处理字典内的数组 字典 数组
cat data.json
{"fruits": ["apple", "banana", "cherry"]
}# 获取字典keys
jq 'keys' data.json ["fruits"
]# 获取获取字典中数组keys
jq '.fruits | keys' data.json
[0,1,2
]# 获取数组中的第一个元素:jq '.fruits[0]' data.json
"apple"# 更新数组中的第二个元素(索引从0开始):jq '.fruits[1] = "blueberry"' data.json# 添加一个新的元素到数组:jq '.fruits += ["date"]' data.json# 删除数组中的最后一个元素:jq 'del(.fruits[-1])' data.json# 遍历数组中的每个元素:jq '.fruits[]' data.json# 使用select过滤器选择包含特定元素的数组:jq '.fruits | select(contains("cherry"))' data.json# 排序数组:jq '.fruits | sort' data.json
root@huang:~# jq --help
jq - commandline JSON processor [version 1.6]
Usage: jq [options]
jq [options] --args
jq [options] --jsonargs
jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.
The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).
For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq
Example:
$ echo '{"foo": 0}' | jq .
{"foo": 0
}
Some of the options include:
-c compact instead of pretty-printed output;
-n use null
as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value
--argjson a v set variable $a to JSON value
--slurpfile a f set variable $a to an array of JSON texts read from
--rawfile a f set variable $a to a string consisting of the contents of
--args remaining arguments are string arguments, not files;
--jsonargs remaining arguments are JSON arguments, not files;
-- terminates argument processing;
Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].
See the manpage for more options.
link
官方链接
https://github.com/jqlang/jq
https://jqlang.github.io/jq/
https://jqplay.org/
shell jq教程 json数据处理
https://blog.csdn.net/cumtglad/article/details/139883987
shell jq命令,解析 json 文件
https://www.cnblogs.com/wutou/p/17652012.html