生成CSV字段和多个数组元素配对

输入示例:

{
  "firstName": "Jam",
  "Product": [
    {
      "productId": "5e09ad38986b7c30f339c5c0"
    },
    {
      "productId": "5e09407b986b7c30f339c18e"
    },
    {
      "productId": "5e094c2a986b7c30f339c1d2"
    }
  ]
}

预期产量:

Jam,5e09ad38986b7c30f339c5c0
Jam,5e09407b986b7c30f339c18e
Jam,5e094c2a986b7c30f339c1d2

当前命令正在生成输出,但在同一行上以逗号分隔:

jq -rc '.firstName,.Product[0] .productId'
评论
痛苦的/呻吟
痛苦的/呻吟

To generate a report in CSV format you need to put column values into an array and pass it to @csv filter.

$ jq -r '[.firstName] + (.Product[] | [.productId]) | @csv' file
"Jam","5e09ad38986b7c30f339c5c0"
"Jam","5e09407b986b7c30f339c18e"
"Jam","5e094c2a986b7c30f339c1d2"
点赞
评论