jqを使用してJSONのヘッダーでCSVを作成する方法

jqを使用してJSONのヘッダーでCSVを作成する方法

jqを使用してjsonヘッダーを含むcsvファイルを作成しようとしています。

次の作品がありますhttps://jqplay.org/s/H_U5mxbTFW

質問:peerings > accept_vpc_info > tenant_id合計値を印刷する方法peerings > accept_vpc_info > vpc_id

私が試したこと:

JSON

{
    "peerings": [
        {
            "accept_vpc_info": {
                "tenant_id": "184a5",
                "vpc_id": "0d11f"
            },
            "created_at": "2018-11-07T08:53:43",
            "description": null,
            "id": "0b19d",
            "name": "NAME1",
            "request_vpc_info": {
                "tenant_id": "08a7b",
                "vpc_id": "1645f"
            },
            "status": "ACTIVE",
            "updated_at": "2018-11-07T09:47:44"
        },
        {
            "accept_vpc_info": {
                "tenant_id": "067eb",
                "vpc_id": "17944"
            },
            "created_at": "2019-06-12T08:29:08",
            "description": null,
            "id": "0d34a",
            "name": "NAME2",
            "request_vpc_info": {
                "tenant_id": "0fd7b",
                "vpc_id": "102c9"
            },
            "status": "ACTIVE",
            "updated_at": "2019-06-12T12:04:56"
        },
        {
            "accept_vpc_info": {
                "tenant_id": "0ae21",
                "vpc_id": "071c4"
            },
            "created_at": "2019-02-21T09:07:35",
            "description": null,
            "id": "173e2",
            "name": "NAME3",
            "request_vpc_info": {
                "tenant_id": "05a21",
                "vpc_id": "0586e"
            },
            "status": "ACTIVE",
            "updated_at": "2019-02-21T09:10:41"
        }
            ]
    }

黄金色の春:

.[] | flatten | map({created_at,description,id,name,status,updated_at}) | (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | join (", ")

とを追加せずにaccept_vpc_info機能しますrequest_vpc_info

私はこれをヘッダーとして追加しようとしました:,,,,,,,,,,,,,,,,,accept_vpc_infoaccept_vpc_info_tenant_idaccept_vpc_info_vpc_idcreated_atdescriptionidnamerequest_vpc_inforequest_vpc_info_tenant_idrequest_vpc_info_vpc_idstatusupdated_at

このような:

"accept_vpc_info__tenant_id","accept_vpc_info__vpc_id","created_at","description","id","name","request_vpc_info__tenant_id","request_vpc_info__vpc_id","status","updated_at"
"184a5","0d11f","2018-11-07T08:53:43","null","0b19d","NAME1","08a7b","1645f","ACTIVE","2018-11-07T09:47:44"
"067eb","17944","2019-06-12T08:29:08","null","0d34a","NAME2","0fd7b","102c9","ACTIVE","2019-06-12T12:04:56"
"0ae21","071c4","2019-02-21T09:07:35","null","173e2","NAME3","05a21","0586e","ACTIVE","2019-02-21T09:10:41"

peerings > accept_vpc_info > tenant_idsum値をどのように印刷できますかpeerings > accept_vpc_info > vpc_id

ありがとう

ベストアンサー1

jqを使用すると、次のことを実行できます。

  1. (一部のフィールドのみを選択すると仮定)jqフィルタ「x.jq」を作成します。
["name","id","tenant","vpc"] ,      ### the csv header
(.peerings[]                        ### for all in peerings list
  | [ .name,
      .id,
      .accept_vpc_info.tenant_id,
      .accept_vpc_info.vpc_id 
    ]
) | @csv
  1. 走る!
$  jq -rf x.jq  ex.json 
"name","id","tenant","vpc"
"NAME1","0b19d","184a5","0d11f"
"NAME2","0d34a","067eb","17944"
"NAME3","173e2","0ae21","071c4"

おすすめ記事