输入 json:
[
{
"name": "cust1",
"grp": [
{
"id": "46",
"name": "BA2"
},
{
"id": "36",
"name": "GA1"
},
{
"id": "47",
"name": "NA1"
},
{
"id": "37",
"name": "TR3"
},
{
"id": "38",
"name": "TS1"
}
]
}
]
如何在 @tsv 输出上获取用空格分隔的 .grp[] 名称?
$ jq -r '.[]|[.name, (.grp//[{"name":"-"}]|.[]|" ",.name)]|@tsv' test_inp
cust1 BA2 GA1 NA1 TR3 TS1
上述示例的预期输出:
cust[TAB]BA2[SPACE]GA1[SPACE]NA1[SPACE]TR3[SPACE]TS1
答案1
jq
方法:
jq -r '.[] | [.name, (.grp | map(.name) | join(" "))] | @tsv' input.json
输出:
cust1 BA2 GA1 NA1 TR3 TS1