如何通过“jq”在一行中打印多个值?

Zha*_* Yi 3 shell json jq

我有如下所示的 json 数据:

{
    "Items": [
        {
            "id": {
                "S": "c921e4eb-5958-424a-ae3a-b9cada0d9481"
            },
            "type": {
                "S": "transaction.1612878877726"
            }
        },
        {
            "id": {
                "S": "355057f0-4327-49c7-979f-5a27410d81ba"
            },
            "type": {
                "S": "transaction.1612345630260"
            }
        },
        {
            "id": {
                "S": "664dc02f-0ad8-484a-98a5-a403beea775b"
            },
            "type": {
                "S": "transaction.1612164919232"
            }
        },
...
  ]
}
Run Code Online (Sandbox Code Playgroud)

我想打印数组中每个项目的值idtype一行Items,例如

c921e4eb-5958-424a-ae3a-b9cada0d9481, transaction.1612878877726
355057f0-4327-49c7-979f-5a27410d81ba, transaction.1612345630260
...
Run Code Online (Sandbox Code Playgroud)

我尝试过cat file | jq '.Items[].id.S, .Items[].type.S',但它打印idtype分行。我怎样才能用jq实现它?

Aar*_*ron 6

我只会使用字符串操作,或者添加 3 个字符串:

jq --raw-output '.Items[] | .id.S + ", " + .type.S' file
Run Code Online (Sandbox Code Playgroud)

或使用字符串插值:

jq --raw-output '.Items[] | "\(.id.S), \(.type.S)"' file
Run Code Online (Sandbox Code Playgroud)

你可以在这里尝试一下