给定这个 json:
{"data": [
{
"account_id": "123",
"account_status": "active",
"name": "john doe",
"email": "[email protected]",
"product_access": []
},
{
"account_id": "345",
"account_status": "active",
"name": "jane doe",
"email": "[email protected]",
"last_active": "2023-08-23T13:03:27.811473590Z",
"product_access": [
{
"name": "Product 1",
"key": "product1",
"url": "acme1.com"
},
{
"name": "Product 1",
"key": "product1",
"url": "acme2.com"
},
{
"name": "Product 1",
"key": "product1",
"url": "acme3.com"
},
{
"name": "Product 2",
"key": "product2",
"url": "acme4.com",
"last_active": "2023-08-23T13:03:27.811473590Z"
},
{
"name": "Product 3",
"key": "product3",
"url": "acme5.com"
},
{
"name": "Product 1",
"key": "product1",
"url": "acme4.com",
"last_active": "2023-08-17T18:21:52.472085713Z"
}
]
}
]
}
我正在尝试编写一个 jq 表达式,以便在 csv 文件中输出如下(为简单起见,此处不添加转义字符):
"account_id", "account_status", "name", "email", "Product 1", "Product 2", "Product 3"
"123", "active", "john doe", "[email protected]", ",,"
"345", "active", "jane doe", "[email protected]", "acme1.com, acme2.com, acme3.com, acme4.com", "acme4.com", "acme5.com"
到目前为止我已经尝试过了...
jq -r '["account_id", "account_status", "name", "email", "product 1", "product 2", "product 3"],
(.data[] | [.account_id, .account_status, .name, .email,
(.product_access[] | select(.key == "product1").url // ""),
(.product_access[] | select(.key == "product2").url // ""),
(.product_access[] | select(.key == "product3").url // "")] | join(",")) | @csv'
它不会将 Product n url 整理到单个单元格中。
您只想将 应用于
join
产品,而不是所有列。您需要将其单独应用于每个产品。尝试这个 :