我在一个名为myfile.json的文件中有一些数据。我需要使用 jq 格式化 - 在 JSON 中它看起来像这样;
{
"result": [
{
"service": "ebsvolume",
"name": "gtest",
"resourceIdentifier": "vol-999999999999",
"accountName": "g-test-acct",
"vendorAccountId": "12345678912",
"availabilityZone": "ap-southeast-2c",
"region": "ap-southeast-2",
"effectiveHourly": 998.56,
"totalSpend": 167.7,
"idle": 0,
"lastSeen": "2018-08-16T22:00:00Z",
"volumeType": "io1",
"state": "in-use",
"volumeSize": 180,
"iops": 2000,
"throughput": 500,
"lastAttachedTime": "2018-08-08T22:00:00Z",
"lastAttachedId": "i-086f957ee",
"recommendations": [
{
"action": "Rightsize",
"preferenceOrder": 2,
"risk": 0,
"savingsPct": 91,
"savings": 189.05,
"volumeType": "gp2",
"volumeSize": 120,
},
{
"action": "Rightsize",
"preferenceOrder": 4,
"risk": 0,
"savingsPct": 97,
"savings": 166.23,
"volumeType": "gp2",
"volumeSize": 167,
},
{
"action": "Rightsize",
"preferenceOrder": 6,
"risk": 0,
"savingsPct": 91,
"savings": 111.77,
"volumeType": "gp2",
"volumeSize": 169,
}
]
}
}
我通过以下方式更好地格式化了它
jq '.result[] | [.service,.name,.resourceIdentifier,.accountName,.vendorAccountId,.availabilityZone,.region,.effectiveHourly,.totalSpend,.idle,.lastSeen,.volumeType,.state,.volumeSize,.iops,.throughput,.lastAttachedTime,.lastAttachedId] |@csv' ./myfile.json
这将获得以下输出;
""ebsvolume","gtest","vol-999999999999","g-test-acct","12345678912","ap-southeast-2c","ap-southeast-2",998.56,167.7,0,"2018-08-16T22:00:00Z","io1","in-use",180,2000,500,"2018-08-08T22:00:00Z","i-086f957ee""
我想通了这一点,但这并不完全是我想要实现的目标。我希望将每个建议列在单独的行下面,而不是在同一行的末尾。
jq '.result[] | [.service,.name,.resourceIdentifier,.accountName,.vendorAccountId,.availabilityZone,.region,.effectiveHourly,.totalSpend,.idle,.lastSeen,.volumeType,.state,.volumeSize,.iops,.throughput,.lastAttachedTime,.lastAttachedId,.recommendations[].action] |@csv' ./myfile.json
这网:
""ebsvolume","gtest","vol-999999999999","g-test-acct","12345678912","ap-southeast-2c","ap-southeast-2",998.56,167.7,0,"2018-08-16T22:00:00Z","io1","in-use",180,2000,500,"2018-08-08T22:00:00Z","i-086f957ee","Rightsize","Rightsize","Rightsize""
我想要的是
""ebsvolume","gtest","vol-999999999999","g-test-acct","12345678912","ap-southeast-2c","ap-southeast-2",998.56,167.7,0,"2018-08-16T22:00:00Z","io1","in-use",180,2000,500,"2018-08-08T22:00:00Z","i-086f957ee",
"Rightsize",
"Rightsize",
"Rightsize""
所以不完全确定如何处理 jq 中"建议"部分中的数组,我认为它可能被称为不扁平化?
你可以试试这个:
jq '.result[] | [ flatten[] | try(.action) // . ] | @csv' file
""ebsvolume","gtest","vol-999999999999","g-test-acct","12345678912","ap-southeast-2c","ap-southeast-2",998.56,167.7,0,"2018-08-16T22:00:00Z","io1","in-use",180,2000,500,"2018-08-08T22:00:00Z","i-086f957ee","Rightsize","Rightsize","Rightsize""
flatten
言出必行。
try
测试.action
是否既不null
也不false
。如果是这样,它将发出其值,否则jq
发出另一个值(运算符//
(。
过滤后的值被放入数组中,以便使用@csv
运算符转换它们。
这对我来说并不过分,实际上它省略了上一个数组中的所有数据 - 但谢谢!
我最终得到了以下内容,因为它没有将 Rightsize 详细信息放在单独的行上,但它必须这样做:
jq -r '.result[] |[.service,.name,.resourceIdentifier,.accountName,.vendorAccountId,.availabilityZone,.region,.effectiveHourly,.totalSpend,.idle,.lastSeen,.volumeType,.state,.volumeSize,.iops,.throughput,.lastAttachedTime,.lastAttachedId,.recommendations[][]] |@csv' ./myfile.json