代码之家  ›  专栏  ›  技术社区  ›  am1991

使JQ输出成为表格

  •  3
  • am1991  · 技术社区  · 6 年前

    我的问题是:如何以表格格式生成JQ输出,用0替换缺少的值?

    因此,JQ的输入是以下弹性搜索JSON响应:

    {"aggregations": {
        "overall": {
            "buckets": [
                {
                    "key": "2018-01-18T00:00:00.000Z-2018-01-25T19:33:16.010Z",
                    "from_as_string": "2018-01-18T00:00:00.000Z",
                    "to": 1516908796010,
                    "to_as_string": "2018-01-25T19:33:16.010Z",
                    "doc_count": 155569,
                    "agg_per_name": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "ASSET-DD583",
                                "doc_count": 3016,
                                "totalMaxUptime_perDays": {
                                    "buckets": [
                                        {
                                            "key_as_string": "2018-01-22T00:00:00.000Z",
                                            "key": 1516579200000,
                                            "doc_count": 161,
                                            "totalMaxUptime": {
                                                "value": 77598
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-23T00:00:00.000Z",
                                            "key": 1516665600000,
                                            "doc_count": 251,
                                            "totalMaxUptime": {
                                                "value": 80789
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-24T00:00:00.000Z",
                                            "key": 1516752000000,
                                            "doc_count": 192,
                                            "totalMaxUptime": {
                                                "value": 56885
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-25T00:00:00.000Z",
                                            "key": 1516838400000,
                                            "doc_count": 2088,
                                            "totalMaxUptime": {
                                                "value": 7392705
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "key": "ASSET-DD568",
                                "doc_count": 2990,
                                "totalMaxUptime_perDays": {
                                    "buckets": [
                                        {
                                            "key_as_string": "2018-01-18T00:00:00.000Z",
                                            "key": 1516233600000,
                                            "doc_count": 106,
                                            "totalMaxUptime": {
                                                "value": 31241
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-19T00:00:00.000Z",
                                            "key": 1516320000000,
                                            "doc_count": 241,
                                            "totalMaxUptime": {
                                                "value": 2952565
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-20T00:00:00.000Z",
                                            "key": 1516406400000,
                                            "doc_count": 326,
                                            "totalMaxUptime": {
                                                "value": 2698235
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-21T00:00:00.000Z",
                                            "key": 1516492800000,
                                            "doc_count": 214,
                                            "totalMaxUptime": {
                                                "value": 85436
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-22T00:00:00.000Z",
                                            "key": 1516579200000,
                                            "doc_count": 279,
                                            "totalMaxUptime": {
                                                "value": 83201
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-23T00:00:00.000Z",
                                            "key": 1516665600000,
                                            "doc_count": 50,
                                            "totalMaxUptime": {
                                                "value": 96467
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-24T00:00:00.000Z",
                                            "key": 1516752000000,
                                            "doc_count": 5,
                                            "totalMaxUptime": {
                                                "value": 903
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-25T00:00:00.000Z",
                                            "key": 1516838400000,
                                            "doc_count": 1769,
                                            "totalMaxUptime": {
                                                "value": 12337946
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "key": "ASSET-42631",
                                "doc_count": 2899,
                                "totalMaxUptime_perDays": {
                                    "buckets": [
                                        {
                                            "key_as_string": "2018-01-18T00:00:00.000Z",
                                            "key": 1516233600000,
                                            "doc_count": 132,
                                            "totalMaxUptime": {
                                                "value": 39054
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-19T00:00:00.000Z",
                                            "key": 1516320000000,
                                            "doc_count": 172,
                                            "totalMaxUptime": {
                                                "value": 47634
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-20T00:00:00.000Z",
                                            "key": 1516406400000,
                                            "doc_count": 214,
                                            "totalMaxUptime": {
                                                "value": 68264
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-21T00:00:00.000Z",
                                            "key": 1516492800000,
                                            "doc_count": 220,
                                            "totalMaxUptime": {
                                                "value": 66243
                                            }
                                        },
                                        {
                                            "key_as_string": "2018-01-25T00:00:00.000Z",
                                            "key": 1516838400000,
                                            "doc_count": 128,
                                            "totalMaxUptime": {
                                                "value": 47660
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
    }
    

    此JSON具有一些固有属性:

    1. agg\U per\U name中的bucket数可变。铲斗
    2. TotalMaxUptime\u/天。buckets表示从当前日期算起的最后7天内每天的组。每天的totalMaxUptime\u。每个资产的桶数将在1到8之间,每个桶对应于特定日期。

    对于给定的示例,JQ的期望输出是一个表格,其中在水平方向上,您有从key\u as\u string开始的日期(在本例中是从2018年1月18日到2018年1月25日),在垂直方向上,有所有资产键(即asset-DD583、asset-DD568等)。表中填充了totalMaxUptime。每个对应日期的值,如果结果中不存在该日期,则应输入“0”值:

    XXXXXXXXXXX, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21, 2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25
    ASSET-DD583, 0,           0,           0,         0,          77598,      80789,      56885,      7392705
    ASSET-DD568, 31241,       2952565,     2698235,   85436,      83201,      96467,      903,        12337946
    ASSET-42631, 39054,       47634,       68264,     66243,      0,          0,          0,          47660
    

    编辑1:

    这就是我取得的成绩:

    input.json | jq '.aggregations.overall.buckets[0].agg_per_name.buckets[] | .key + ", " + (.totalMaxUptime_perDays.buckets[] | .key_as_string + ", " + (.totalMaxUptime.value | tostring))' | sed 's/"//g' | sed 's/T00:00:00.000Z//g'> uptime.csv
    

    产生这种输出:

    ASSET-DD583, 2018-01-22, 77598
    ASSET-DD583, 2018-01-23, 80789
    ASSET-DD583, 2018-01-24, 56885
    ASSET-DD583, 2018-01-25, 7392705
    ...............
    
    4 回复  |  直到 6 年前
        1
  •  7
  •   peak    6 年前

    在下面,我使用了 @tsv 因此,可以更容易地将输出视为一个表,但您可能希望使用 @csv .

    这里最棘手的部分是把0放在正确的位置。创建JSON“dictionary”(即JSON对象)使其变得简单。在这里 normalize 利用jq将遵守向对象添加键的顺序这一事实。

    def dates:
      ["2018-01-18", "2018-01-19", "2018-01-20", "2018-01-21", "2018-01-22", "2018-01-23", "2018-01-24", "2018-01-25"];
    
    def normalize:
      . as $in 
      | reduce dates[] as $k ({}; .[$k] = ($in[$k] // 0));
    
    (["Asset"] + dates),
    (.aggregations.overall.buckets[].agg_per_name.buckets[]
     | .key as $asset
     | .totalMaxUptime_perDays.buckets
     | map( { (.key_as_string | sub("T.*";"") ): .totalMaxUptime.value } ) 
     | add
     | normalize
     | [$asset] + [.[]]
     )
    | @tsv
    

    您可能需要修改上述内容,以便 dates 根据数据计算。

    输出:

    Asset   2018-01-18  2018-01-19  2018-01-20  2018-01-21  2018-01-22  2018-01-23  2018-01-24  2018-01-25
    ASSET-DD583 0   0   0   0   77598   80789   56885   7392705
    ASSET-DD568 31241   2952565 2698235 85436   83201   96467   903 12337946
    ASSET-42631 39054   47634   68264   66243   0   0   0   47660
    

    编辑: 周围的括号 $in[$k] // 0 已添加。

        2
  •  4
  •   charlietango    6 年前

    部分解决您的问题。

    如果使用 @csv .

    例如,假设您有

    {
      "a": [1,2,3],
      "b": [
        {
          "x": 10
        },
        {
          "x": 20
        },
        {
          "x": 30
        }
      ]
    }
    

    获取 1,2,3 您应该使用 jq '.a | @csv'

    获取 10,20,30 您应该使用 jq '[.b[].x] | @csv'

    希望这有帮助!

        3
  •  1
  •   ivcubr Freak    5 年前

    请尝试以下操作:

    cat input.json
        | jq '.aggregations.overall.buckets[0].agg_per_name.buckets[] |
        .key + ", " + (.totalMaxUptime_perDays.buckets[] |
        .key_as_string + ", " + (.totalMaxUptime.value | tostring))' |column -t -s,
    
        4
  •  0
  •   shoma    3 年前

    这有一个工具。 https://github.com/uzimaru0000/tv

    $ curl -s https://jsonplaceholder.typicode.com/users | tv
    +--+------------------------+----------------+-------------------------+-------+---------------------+-------------+-------+
    |id|name                    |username        |email                    |address|phone                |website      |company|
    +--+------------------------+----------------+-------------------------+-------+---------------------+-------------+-------+
    |1 |Leanne Graham           |Bret            |Sincere@april.biz        |...    |1-770-736-8031 x56442|hildegard.org|...    |
    |2 |Ervin Howell            |Antonette       |Shanna@melissa.tv        |...    |010-692-6593 x09125  |anastasia.net|...    |
    |3 |Clementine Bauch        |Samantha        |Nathan@yesenia.net       |...    |1-463-123-4447       |ramiro.info  |...    |
    |4 |Patricia Lebsack        |Karianne        |Julianne.OConner@kory.org|...    |493-170-9623 x156    |kale.biz     |...    |
    |5 |Chelsey Dietrich        |Kamren          |Lucio_Hettinger@annie.ca |...    |(254)954-1289        |demarco.info |...    |
    |6 |Mrs. Dennis Schulist    |Leopoldo_Corkery|Karley_Dach@jasper.info  |...    |1-477-935-8478 x6430 |ola.org      |...    |
    |7 |Kurtis Weissnat         |Elwyn.Skiles    |Telly.Hoeger@billy.biz   |...    |210.067.6132         |elvis.io     |...    |
    |8 |Nicholas Runolfsdottir V|Maxime_Nienow   |Sherwood@rosamond.me     |...    |586.493.6943 x140    |jacynthe.com |...    |
    |9 |Glenna Reichert         |Delphine        |Chaim_McDermott@dana.io  |...    |(775)976-6794 x41206 |conrad.com   |...    |
    |10|Clementina DuBuque      |Moriah.Stanton  |Rey.Padberg@karina.biz   |...    |024-648-3804         |ambrose.net  |...    |
    +--+------------------------+----------------+-------------------------+-------+---------------------+-------------+-------+