Max*_*Max 5 json nested key path jq
我想要做的是在我的 json 上“行走”,并为每个名为“base”的键添加一个键,该键是该键的完整“路径”。
这是我的 JSON:
{
"entity": {
"product": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": ["product", "products", "pdt", "pdts"]
}
},
"rabbit": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": ["rabbit", "rabbits"]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要这样的结果:
{
"entity": {
"product": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": ["product", "products", "pdt", "pdts"],
"path": "entity.product.title"
}
},
"rabbit": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": ["rabbit", "rabbits"]
"path": "entity.rabbit.title"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,我实现了这一点,但我只得到一个子密钥:
walk(if type == "object" and .base then keys[] as $k | .base |= {path: $k} else . end)
Run Code Online (Sandbox Code Playgroud)
结果:
{
"entity": {
"product": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": {
"path": "base"
}
}
},
"rabbit": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": {
"path": "base"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个:
walk(if type == "object" and .base then paths(..) as $v | .base |= {path: $v} else . end)
Run Code Online (Sandbox Code Playgroud)
结果:
{
"entity": {
"product": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": {
"path": [
"base",
3
]
}
}
},
"rabbit": {
"att": {
"number_of_values": "Number of values"
},
"title": {
"base": {
"path": [
"base",
1
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的建议和专业知识!
这是一种方法:
reduce paths(objects | has("base")) as $p (.;
setpath($p + ["path"]; $p | join("."))
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |