关于json
1 jq
1.1安装
yum install -y jq
1.2简单介绍
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; #使用“null”作为单个输入值
-e set the exit status code based on the output; #基于所述输出设置所述退出状态代码;
-s read (slurp) all inputs into an array; apply filter to it; #将所有输入读取(slurp)到一个数组中;对其应用过滤器;
-r output raw strings, not JSON texts; #输出原始字符串,而不是JSON文本;
-R read raw strings, not JSON texts; #读取原始字符串,而不是JSON文本;
-C colorize JSON; #将JSON着色;
-M monochrome (don't colorize JSON); #单色(不要将JSON着色);
-S sort keys of objects on output; #对输出对象的键进行排序;
--tab use tabs for indentation; #使用制表符进行缩进;
--arg a v set variable $a to value <v>; #将变量$a设置为值<v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
--rawfile a f set variable $a to a string consisting of the contents of <f>;
--args remaining arguments are string arguments, not files; #剩下的参数是字符串参数,而不是文件;
--jsonargs remaining arguments are JSON arguments, not files; #剩下的参数是JSON参数,而不是文件;
-- 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.
1.3简单使用
1.3.1如何使用 JQ 命令组织 JSON 数据
cat employee.json
{"workers":{"name": "John Brooks","id": "003"}}
jq '.' employee.json
{
"workers": {
"name": "John Brooks",
"id": "003"
}
}
1.3.2如何使用 JQ 命令访问属性
jq '.workers' employee.json
{
"name": "John Brooks",
"id": "003"
}
jq '.workers.name' employee.json
"John Brooks"
1.3.3如何使用 JQ 命令访问数组项
我们还可以使用 .[] 运算符访问和输出 JSON 文件中数组中存在的元素。对于这个例子,我们将修改我们的 JSON 文件,添加下面内容:
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
cat employee1.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
jq .[] employee1.json
{
"name": "John Brooks",
"id": "003"
}
{
"name": "Randy Park",
"id": "053"
}
{
"name": "Todd Gray",
"id": "009"
}
要仅输出第二个数组,我们可以通过以下方式修改上述命令:
jq .[1] employee1.json
{
"name": "Randy Park",
"id": "053"
}
请记住,数组从索引 0 开始的。
我们还可以使用 .字段 运算符访问数组中存在的属性。例如,如果我们想访问第三个数组中的 name 属性,那么我们将运行以下命令
jq '.[2].name' employee1.json
"Todd Gray"
类似地,要访问数组中的所有名称属性,我们可以执行以下命令:
jq '.[].name' employee.json
"John Brooks"
"Randy Park"
"Todd Gray"
2 docker inspect 对jq的使用
2.1使用步骤
2.1.1把数据变成json格式
docker inspect wordpress |jq
2.1.2得要想要的数据
docker inspect wordpress |jq .[].Id
"sha256:c3c92cc3dcb1a903fed0374a837f38d716ae104d0e4c9705bddb53a76419534d"
2.2 docker inspect nginx --format '{{.Id}}'
docker inspect $(容器id) --format '{{.State.Pid}}, {{.Id}}, {{.Name}}, {{.GraphDriver}}'
2.3 万能grep
docker inspect nginx |grep -i id
"Id": "sha256:080ed0ed8312deca92e9a769b518cdfa20f5278359bd156f3469dd8fa532db6b",
3日志
####3.1获取某个时间以后的日志
docker logs --since 2023-04-04T00:00:00
####3.2获取实时的日志
docker logs -f
####3.3获取最后300行的日志
docker logs -f --tail=300
####3.3获取某个时间段的日志
docker logs --since 2023-04-04T00:00:00 --until 2023-04-05T00:00:00