简体   繁体   English

使用 jq 将 JSON 解析为 CSV 但在多个记录中拆分子列表

[英]Parse JSON to CSV using jq but split sub list in multiple records

I am parsing a JSON result which i get from the Azure RateAPI and want to convert it into a standard CSV file.我正在解析从 Azure RateAPI 获得的 JSON 结果,并希望将其转换为标准 CSV 文件。

The following line is what i am using to convert it into CSV and it works but as one of the attributes is a list, it does not provide me the result i am seeking.以下行是我用来将其转换为 CSV 的内容,它可以工作,但由于属性之一是列表,它没有提供我正在寻找的结果。 For every item in the "sub list", i would need to create another record in my csv file.对于“子列表”中的每个项目,我需要在我的 csv 文件中创建另一条记录。

cat myfile.json | jq -r '.Meters[] | [ .EffectiveDate, .IncludedQuantity, .MeterCategory, .MeterId, .MeterName, .MeterRates[], .MeterRegion, .MeterStatus, .MeterSubCategory, .MeterTags[], .Units] | @csv'

Here are 3 records I am trying to parse.这是我要解析的 3 条记录。 I am having trouble with record 2 because MeterRates is actually the list where i need both, the attribute and the value.我在使用记录 2 时遇到问题,因为 MeterRates 实际上是我需要属性和值的列表。 I would need record 2, once parsed, to correspond to 3 records in the CSV file where each record contains one item of the list in the MeterRates.我需要记录 2,一旦解析,对应于 CSV 文件中的 3 条记录,其中每条记录包含 MeterRates 列表中的一项。 An example of expected result is at the end预期结果的示例在末尾

  "OfferTerms": [],
  "Meters": [
    {
      "EffectiveDate": "2019-03-01T00:00:00Z",
      "IncludedQuantity": 0,
      "MeterCategory": "Virtual Machines",
      "MeterId": "d0bf9053-17c4-4fec-8502-4eb8376343a7",
      "MeterName": "F2/F2s Low Priority",
      "MeterRates": {
        "0": 0.0766
      },
      "MeterRegion": "US West 2",
      "MeterStatus": "Active",
      "MeterSubCategory": "F/FS Series Windows",
      "MeterTags": [],
      "Unit": "1 Hour"
    },
    {
      "EffectiveDate": "2014-11-01T00:00:00Z",
      "IncludedQuantity": 0,
      "MeterCategory": "Azure DevOps",
      "MeterId": "c4d6fa88-0df9-4680-867a-b13c960a875f",
      "MeterName": "Virtual User Minute",
      "MeterRates": {
        "0": 0.0004,
        "1980000": 0.0002,
        "9980000": 0.0001
      },
      "MeterRegion": "",
      "MeterStatus": "Active",
      "MeterSubCategory": "Cloud-Based Load Testing",
      "MeterTags": [],
      "Unit": "1/Month"
    },
    {
      "EffectiveDate": "2017-04-01T00:00:00Z",
      "IncludedQuantity": 0,
      "MeterCategory": "SQL Database",
      "MeterId": "cb770eab-d5c8-45fd-ac56-8c35069f5a29",
      "MeterName": "P4 DTUs",
      "MeterRates": {
        "0": 68.64
      },
      "MeterRegion": "IN West",
      "MeterStatus": "Active",
      "MeterSubCategory": "Single Premium",
      "MeterTags": [],
      "Unit": "1/Day"
    }
    ]
}

Actual results using the code i provided is the following:使用我提供的代码的实际结果如下:

"2019-03-01T00:00:00Z",0,"Virtual Machines","d0bf9053-17c4-4fec-8502-4eb8376343a7","F2/F2s Low Priority",0.0766,"US West 2","Active","F/FS Series Windows",
"2014-11-01T00:00:00Z",0,"Azure DevOps","c4d6fa88-0df9-4680-867a-b13c960a875f","Virtual User Minute",0.0004,0.0002,0.0001,"","Active","Cloud-Based Load Testing",
"2017-04-01T00:00:00Z",0,"SQL Database","cb770eab-d5c8-45fd-ac56-8c35069f5a29","P4 DTUs",68.64,"IN West","Active","Single Premium",

but the result i would expect is (record 2 to correspond to 3 records in the CSV file based on MeterRates):但我期望的结果是(记录 2 对应于基于 MeterRates 的 CSV 文件中的 3 条记录):

"2019-03-01T00:00:00Z",0,"Virtual Machines","d0bf9053-17c4-4fec-8502-4eb8376343a7","F2/F2s Low Priority",0,0.0766,"US West 2","Active","F/FS Series Windows",
"2014-11-01T00:00:00Z",0,"Azure DevOps","c4d6fa88-0df9-4680-867a-b13c960a875f","Virtual User Minute",0,0.0004,"","Active","Cloud-Based Load Testing",
"2014-11-01T00:00:00Z",0,"Azure DevOps","c4d6fa88-0df9-4680-867a-b13c960a875f","Virtual User Minute",1980000,0.0002,"","Active","Cloud-Based Load Testing",
"2014-11-01T00:00:00Z",0,"Azure DevOps","c4d6fa88-0df9-4680-867a-b13c960a875f","Virtual User Minute",9980000,0.0001"","Active","Cloud-Based Load Testing",
"2017-04-01T00:00:00Z",0,"SQL Database","cb770eab-d5c8-45fd-ac56-8c35069f5a29","P4 DTUs",0,68.64,"IN West","Active","Single Premium",

Thank you for your help.谢谢您的帮助。

You'll want to add a step between getting Meters items and outputting rows, to output the various combinations of Meters items with different rates.您需要在获取Meters项目和输出行之间添加一个步骤,output 以不同速率的Meters项目的各种组合。 As you have it right now, you're outputting the rates as other items for the row which isn't really what you want.正如您现在所拥有的那样,您正在将费率作为该行的其他项目输出,这并不是您真正想要的。

In this case, you could just add a new property to hold the value of the corresponding MeterRate .在这种情况下,您只需添加一个新属性来保存相应MeterRate的值。

.Meters[] | .MeterRate = (.MeterRates | to_entries[])
    | [.EffectiveDate, .IncludedQuantity, .MeterCategory, .MeterId , .MeterName,
       .MeterRate.key, .MeterRate.value,
       .MeterRegion, .MeterStatus, .MeterSubCategory, .MeterTags[], .Units]
    | @csv

You may want to consider doing something similar for the MeterTags items so you don't end up with potentially random column counts.您可能需要考虑为MeterTags项目做类似的事情,这样您就不会得到潜在的随机列数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM