使用 jq 将 JSON 解析为 CSV,但在多个记录中拆分子列表
Parse JSON to CSV using jq but split sub list in multiple records
我正在解析从 Azure RateAPI 获得的 JSON 结果,并希望将其转换为标准 CSV 文件。
下一行是我用来将它转换为 CSV 的内容,它可以工作,但由于其中一个属性是一个列表,它没有提供我正在寻找的结果。对于 "sub list" 中的每一项,我需要在我的 csv 文件中创建另一条记录。
cat myfile.json | jq -r '.Meters[] | [ .EffectiveDate, .IncludedQuantity, .MeterCategory, .MeterId, .MeterName, .MeterRates[], .MeterRegion, .MeterStatus, .MeterSubCategory, .MeterTags[], .Units] | @csv'
这是我要解析的 3 条记录。我在处理记录 2 时遇到问题,因为 MeterRates 实际上是我同时需要属性和值的列表。我需要记录 2,一旦解析,对应于 CSV 文件中的 3 条记录,其中每条记录包含 MeterRates 列表中的一项。预期结果的例子在最后
"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"
}
]
}
使用我提供的代码的实际结果如下:
"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",
但我期望的结果是(记录 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",
感谢您的帮助。
您需要在获取 Meters
项和输出行之间添加一个步骤,以输出具有不同比率的 Meters
项的各种组合。正如您现在所拥有的那样,您正在将费率作为该行的其他项目输出,这并不是您真正想要的。
在这种情况下,您可以只添加一个新的 属性 来保存相应 MeterRate
的值。
.Meters[] | .MeterRate = (.MeterRates | to_entries[])
| [.EffectiveDate, .IncludedQuantity, .MeterCategory, .MeterId , .MeterName,
.MeterRate.key, .MeterRate.value,
.MeterRegion, .MeterStatus, .MeterSubCategory, .MeterTags[], .Units]
| @csv
您可能需要考虑对 MeterTags
项执行类似的操作,这样您就不会得到可能随机的列数。
我正在解析从 Azure RateAPI 获得的 JSON 结果,并希望将其转换为标准 CSV 文件。
下一行是我用来将它转换为 CSV 的内容,它可以工作,但由于其中一个属性是一个列表,它没有提供我正在寻找的结果。对于 "sub list" 中的每一项,我需要在我的 csv 文件中创建另一条记录。
cat myfile.json | jq -r '.Meters[] | [ .EffectiveDate, .IncludedQuantity, .MeterCategory, .MeterId, .MeterName, .MeterRates[], .MeterRegion, .MeterStatus, .MeterSubCategory, .MeterTags[], .Units] | @csv'
这是我要解析的 3 条记录。我在处理记录 2 时遇到问题,因为 MeterRates 实际上是我同时需要属性和值的列表。我需要记录 2,一旦解析,对应于 CSV 文件中的 3 条记录,其中每条记录包含 MeterRates 列表中的一项。预期结果的例子在最后
"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"
}
]
}
使用我提供的代码的实际结果如下:
"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",
但我期望的结果是(记录 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",
感谢您的帮助。
您需要在获取 Meters
项和输出行之间添加一个步骤,以输出具有不同比率的 Meters
项的各种组合。正如您现在所拥有的那样,您正在将费率作为该行的其他项目输出,这并不是您真正想要的。
在这种情况下,您可以只添加一个新的 属性 来保存相应 MeterRate
的值。
.Meters[] | .MeterRate = (.MeterRates | to_entries[])
| [.EffectiveDate, .IncludedQuantity, .MeterCategory, .MeterId , .MeterName,
.MeterRate.key, .MeterRate.value,
.MeterRegion, .MeterStatus, .MeterSubCategory, .MeterTags[], .Units]
| @csv
您可能需要考虑对 MeterTags
项执行类似的操作,这样您就不会得到可能随机的列数。