在对象数组中展平或 "explode" 数组值字段?
Flatten or "explode" an array-valued field, in an array of objects?
我有这个数据:
[
{ "path": "x", "types": ["a", "b"] },
{ "path": "y", "types": ["c", "d"] }
]
我希望获得此输出:
{ "path": "x", "type": "a" }
{ "path": "x", "type": "b" }
{ "path": "y", "type": "c" }
{ "path": "y", "type": "d" }
我如何使用 Jq 执行此操作?
尝试
.[] | {path, type: .types[]}
{"path":"x","type":"a"}
{"path":"x","type":"b"}
{"path":"y","type":"c"}
{"path":"y","type":"d"}
使用 --compact-output
或 -c
选项也可以获得相同的 compact 格式。
编辑:
This is documented in the manual under the heading "Object Construction: {}
". Specifically the statement, "If one of the expressions produces multiple results, multiple dictionaries will be produced."
感谢 查找。
我有这个数据:
[
{ "path": "x", "types": ["a", "b"] },
{ "path": "y", "types": ["c", "d"] }
]
我希望获得此输出:
{ "path": "x", "type": "a" }
{ "path": "x", "type": "b" }
{ "path": "y", "type": "c" }
{ "path": "y", "type": "d" }
我如何使用 Jq 执行此操作?
尝试
.[] | {path, type: .types[]}
{"path":"x","type":"a"}
{"path":"x","type":"b"}
{"path":"y","type":"c"}
{"path":"y","type":"d"}
使用 --compact-output
或 -c
选项也可以获得相同的 compact 格式。
编辑:
This is documented in the manual under the heading "Object Construction:
{}
". Specifically the statement, "If one of the expressions produces multiple results, multiple dictionaries will be produced."
感谢