[英]How to print path and key values of JSON file using JQ
I would like to print each path and value of a json file with included key values line by line.我想逐行打印包含键值的json文件的每个路径和值。 I would like the output to be comma delimited or at least very easy to cut and sort using Linux command line tools.
我希望输出以逗号分隔或至少很容易使用 Linux 命令行工具进行剪切和排序。 Given the following json and jq , I have been given jq code which seems to do this for the test JSON, but I am not sure it works in all cases or is the proper approach.
鉴于以下json和jq ,我得到了jq代码,它似乎对测试 JSON 执行此操作,但我不确定它是否适用于所有情况或是否是正确的方法。
Is there a function in jq which does this automatically? jq 中是否有自动执行此操作的函数? If not, is there a "most concise best way" to do it?
如果没有,是否有“最简洁的最佳方法”来做到这一点?
My wish would be something like:我的愿望是这样的:
$ cat short.json | jq -doit '.'
Reservations,0,Instances,0,ImageId,ami-a
Reservations,0,Instances,0,InstanceId,i-a
Reservations,0,Instances,0,InstanceType,t2.micro
Reservations,0,Instances,0,KeyName,ubuntu
Test JSON:测试 JSON:
$ cat short.json | jq '.'
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"ImageId": "ami-a",
"InstanceId": "i-a",
"InstanceType": "t2.micro",
"KeyName": "ubuntu"
}
]
}
]
}
Code Recommended:代码推荐:
https://unix.stackexchange.com/questions/561460/how-to-print-path-and-key-values-of-json-file https://unix.stackexchange.com/questions/561460/how-to-print-path-and-key-values-of-json-file
Supporting:配套:
https://unix.stackexchange.com/questions/515573/convert-json-file-to-a-key-path-with-the-resulting-value-at-the-end-of-each-k https://unix.stackexchange.com/questions/515573/convert-json-file-to-a-key-path-with-the-resulting-value-at-the-end-of-each-k
JQ Code Too long and complicated! JQ 代码太长太复杂了!
jq -r '
paths(scalars) as $p
| [ ( [ $p[] | tostring ] | join(".") )
, ( getpath($p) | tojson )
]
| join(": ")
' short.json
Result:
Reservations.0.Instances.0.ImageId: "ami-a"
Reservations.0.Instances.0.InstanceId: "i-a"
Reservations.0.Instances.0.InstanceType: "t2.micro"
Reservations.0.Instances.0.KeyName: "ubuntu"
A simple jq query to achieve the requested format:一个简单的 jq 查询来实现请求的格式:
paths(scalars) as $p
| $p + [getpath($p)]
| join(",")
If your jq is ancient and you cannot upgrade, insert | map(tostring)
如果你的jq很老,无法升级,插入
| map(tostring)
| map(tostring)
before the last line above. | map(tostring)
在上面最后一行之前。
Reservations,0,Instances,0,ImageId,ami-a
Reservations,0,Instances,0,InstanceId,i-a
Reservations,0,Instances,0,InstanceType,t2.micro
Reservations,0,Instances,0,KeyName,ubuntu
If a key or atomic value contains "," then of course using a comma may be inadvisable.如果键或原子值包含“,”,那么当然不建议使用逗号。 For this reason, it might be preferable to use a character such as TAB that cannot appear in a JSON key or atomic value.
因此,最好使用不能出现在 JSON 键或原子值中的字符,例如 TAB。 Consider therefore using
@tsv
:因此考虑使用
@tsv
:
paths(scalars) as $p
| $p + [getpath($p)]
| @tsv
(The comment above about ancient versions of jq applies here too.) (上面关于 jq 古代版本的评论也适用于此。)
将其作为流阅读。
$ jq --stream -r 'select(.[1]|scalars!=null) | "\(.[0]|join(".")): \(.[1]|tojson)"' short.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.