如何使用jq提取所有(也嵌套的)键名 [英] How to extract all (also nested) key names with jq
本文介绍了如何使用jq提取所有(也嵌套的)键名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使在带有jq的嵌套对象中,如何提取所有键名? 例如,我有json:
How can I extract all key names, even in nested objects with jq? For example, I have json:
{
"a": 1,
"b": {
"c": 2
}
}
{
"a": 1,
"b": {
"c": 2
}
}
,我想获取列表: a,b,b.c
and I want to get list: a, b, b.c
我知道对于顶级密钥,我可以通过以下方式获得它:
. | to_entries[] | .key
,但是嵌套对象中的键呢?
I know that for top level keys I can get this, with:
. | to_entries[] | .key
, but what about keys in nested objects?
推荐答案
短 jq 解决方案:
jq -r '[paths | join(".")]' jsonfile
输出:
[
"a",
"b",
"b.c"
]
-
paths
函数将路径输入到其输入中的所有元素
paths
function outputs the paths to all the elements in its inputjoin(".")
-在层级路径内串联键join(".")
- to concatenate keys within hierarchical paths这篇关于如何使用jq提取所有(也嵌套的)键名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文