【发布时间】:2018-04-04 10:14:44
【问题描述】:
假设我有一个像这样的 JSON 文件的类别树:
[
{
"id": "1",
"text": "engine",
"children": [
{
"id": "2",
"text": "exhaust",
"children": []
},
{
"id": "3",
"text": "cooling",
"children": [
{
"id": "4",
"text": "cooling fan",
"children": []
},
{
"id": "5",
"text": "water pump",
"children": []
}
]
}
]
},
{
"id": "6",
"text": "frame",
"children": [
{
"id": "7",
"text": "wheels",
"children": []
},
{
"id": "8",
"text": "brakes",
"children": [
{
"id": "9",
"text": "brake calipers",
"children": []
}
]
},
{
"id": "10",
"text": "cables",
"children": []
}
]
}
]
我怎样才能把它转换成这个平面表?
id parent_id text
1 NULL engine
2 1 exhaust
3 1 cooling
4 3 cooling fan
5 3 water pump
6 NULL frame
7 6 wheels
8 6 brakes
9 8 brake calipers
10 6 cables
我发现了类似的问题和倒置的问题(从表格到 JSON),但我无法用 jq 及其@tsv 过滤器弄清楚。我还注意到答案中不经常引用“扁平化”过滤器(虽然它看起来正是我需要的工具),但这可能是因为它是最近在最新版本的 jq 中引入的。
【问题讨论】:
标签: json csv recursion jq flatten