我的测试json文件如下:
{
"server":"xxx",
"cert":"line1\nline2",
"security/path": "/var/log/systems.log"
}
我想按 key 进行过滤security/path
,下面的命令都不起作用。
jq .security/path test.json
jq: error: path/0 is not defined at <top-level>, line 1:
.security/path
jq: 1 compile error
jq '.security/path' test.json
有相同的结果。
答案1
从基本过滤器:
对象标识符索引:
...
如果键包含特殊字符或以数字开头,则需要用双引号将其括起来,如下所示:
."foo$"
,否则.["foo$"]
....
所以,
$ jq -r '."security/path"' test.json
/var/log/systems.log
答案2
如果你的输出是这样的,
"annotations": {
"eks.amazonaws.com/role-arn": "arn:aws:iam::907636088649:role/stg-eks_helm-chart",
"meta.helm.sh/release-name": "helm-chart-base",
"meta.helm.sh/release-namespace": "stg-helm-chart"
}
以下将起作用,
jq -r '.metadata.annotations["eks.amazonaws.com/role-arn"]'
并将产生如下输出,
arn:aws:iam::907636088649:role/stg-eks_helm-chart