我有一个JSON对象,它仅包含字符串和其他对象:
{
"key1": "value1",
"key2": "value2",
"key3": {
"subKey1":"value3",
"subKey2":"value4"
}
}
我想通过展平结构将其转换为字符串数组
[
"key1.value1",
"key2.value2",
"key3.subKey1.value3",
"key3.subKey2.value4",
]
我对jq语法还很陌生,到目前为止,我在的帮助下设法做到了这一点
to_entries | map([.key, .value | if type == "object" then type else . end]|join("."))
然后我得到
[
"key1.value1",
"key2.value2",
"key3.object"
]
答案 0 :(得分:2)
替代方法:
jq '[paths(scalars) as $p | $p + [getpath($p)] | join(".")]' f.json
预期输出:
[
"key1.value1",
"key2.value2",
"key3.subKey1.value3",
"key3.subKey2.value4"
]
答案 1 :(得分:1)
使用paths
使任务变得简单。
paths as $p
| getpath($p)
| scalars
| $p + [.]
| join(".")
产生流:
"key1.value1"
"key2.value2"
"key3.subKey1.value3"
"key3.subKey2.value4"
因此,您需要将上方的过滤器括在方括号中以产生一个数组。