我有一个json文件,我希望检索所有键的所有路径。我一直在使用JQ,但是,它不显示以空为值的路径。我遗漏了什么?
Json:
{
"Root": [
{
"id1": "val",
"id2": "val",
"id3": null,
"id4": 1,
"id5": null,
"id6": "val",
"id7": {
"id8": "val",
"id9": "val",
"id10": null,
"id11": "val"
}
}
]
}
jq -r 'paths(scalars) as $p | [ ( [ $p[] | tostring ] | join(".") ), ( getpath($p) | tojson )] | join(": ")' test_data.json
输出如
所示{
"Root": [
{
"id1": "val",
"id2": "val",
"id3": null,
"id4": 1,
"id5": null,
"id6": "val",
"id7": {
"id8": "val",
"id9": "val",
"id10": null,
"id11": "val"
}
}
]
}
我希望看到所有节点的路径,包括那些值为null的节点。
这个问题在jq FAQ中有涉及,其中给出了paths
更包容的版本的以下定义:
def allpaths:
def conditional_recurse(f): def r: ., (select(.!=null) | f | r); r;
path(conditional_recurse(.[]?)) | select(length > 0);
对应的paths/1
版本为:
def allpaths(filter):
allpaths as $p | getpath($p) as $v | select($v | filter) | $p;