使用jq将json输出到表

问题描述 投票:0回答:1

我正在尝试使用 jq 将 json 输出转换为表格式。

这是我的输入

{
   "eTag" : null,
   "id" : "providers/Microsoft.Billing/billingAccounts/8000000/providers/Microsoft.CostManagement/query/7acd05b7-5aff-49df-b7db-4d7f111eecf1",
   "location" : null,
   "name" : "7acd05b7-5aff-49df-b7db-4d7f111eecf1",
   "properties" : {
      "columns" : [
         {
            "name" : "Cost",
            "type" : "Number"
         },
         {
            "name" : "UsageDate",
            "type" : "Number"
         },
         {
            "name" : "CostStatus",
            "type" : "String"
         },
         {
            "name" : "Currency",
            "type" : "String"
         }
      ],
      "nextLink" : null,
      "rows" : [
         
         [
            15.87,
            20230818,
            "Cost",
            "USD"
         ],
         [
            17.00,
            20230819,
            "Cost",
            "USD"
         ],
         [
            16.27,
            20230820,
            "Cost",
            "USD"
         ]
      ]
   },
   "type" : "Microsoft.CostManagement/query"
}

我想要的输出

Cost | UsageDate 
----------------
15.87  | 20230818 
17.00  | 20230818
16.27  | 20230818 

我正在尝试使用 jq 到目前为止,我只得到了所需的输出格式,但没有得到所需的输出格式。您能否分享 jq 过滤器查询以获得所需的输出

.properties | (.columns[].name, .rows[])

arrays json jq tabular
1个回答
1
投票

这是您可以借鉴的一个要点,尤其是完善您的问题中未完全说明的一些视觉方面。如果您只需要前两列,请使用

.columns[:2]
.rows[][:2]

jq -r '.properties
  | (.columns | map(.name) | join(" | ") | ., gsub("."; "-"))
  , (.rows[] | join(" | "))
'
Cost | UsageDate | CostStatus | Currency
----------------------------------------
15.87 | 20230818 | Cost | USD
17 | 20230819 | Cost | USD
16.27 | 20230820 | Cost | USD

演示

© www.soinside.com 2019 - 2024. All rights reserved.