我有一些数据看起来像这样
{
"type1": [
"a", "b"
],
"type2": [
"c", "d"
],
"type3": "x"
}
我希望你能把它转换成
[
{"value": "a", "type": "type1" },
{"value": "b", "type": "type1" },
{"value": "c", "type": "type2" },
{"value": "d", "type": "type2" },
{"value": "x", "type": "type3" },
]
数组元素可以有一个或多个值。使用jq,这是否可能?
我已经处理了非数组元素,但我的表达式仅保留列表中的最后一项。
to_entries
| map_values({value:[.value]|flatten, type:.key})
| map_values({value:.value[], type:.type})
我得到了这个结果,其中元素“b”和“d”被排除在结果之外。
[
{
"value": "a",
"type": "type1"
},
{
"value": "c",
"type": "type2"
},
{
"value": "x",
"type": "type3"
}
]