【问题标题】:Formatting into CSV JSON file using jq使用 jq 格式化为 CSV JSON 文件
【发布时间】:2018-08-21 08:23:29
【问题描述】:

我在名为 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中“recommendations”部分中的数组,我认为它可能被称为unflattening?

【问题讨论】:

    标签: arrays json formatting jq


    【解决方案1】:

    你可以试试这个:

    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 运算符进行转换。

    【讨论】:

      【解决方案2】:

      这对我来说并不过分,实际上它省略了前一个数组中的所有数据 - 但谢谢!

      我最终得到了以下结果,尽管它没有将 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多