如何使用 jq 将 JSON 字符串格式化为表格?

如何使用 jq 将 JSON 字符串格式化为表格?

我需要使用 jq 将 JSON 字符串转换为表格格式,以便在终端中显示输出,如下所示。

{
  "results": [
    [
      {
        "field": "@timestamp",
        "value": "2023-07-03 08:28:00.000"
      },
      {
        "field": "CpuReserved",
        "value": "8192.0"
      },
      {
        "field": "CpuUtilized",
        "value": "4056.412942708333"
      },
      {
        "field": "MemoryReserved",
        "value": "61440"
      },
      {
        "field": "MemoryUtilized",
        "value": "4311"
      },
      {
        "field": "@ptr",
        "value": "CpABClUKUTE0NjcxNzAzNzI0NzovYXdzL2Vjcy9jb250YWluZXJpbnNpZ2h0cy9pcmwtaW5mcmEtc2hhcmVkLWplbmtpbnMtbWFpbi9wZXJmb3JtYW5jZRABEjUaGAIGSaxosQAAAAAd+TFeAAZKKHIwAAABEiABKIDF9taRMTDA7v3WkTE4BkC9NEjNPFCBIRgAEAAYAQ=="
      }
    ],
    [
      {
        "field": "@timestamp",
        "value": "2023-07-03 08:28:00.000"
      },
      {
        "field": "CpuReserved",
        "value": "8192.0"
      },
      {
        "field": "CpuUtilized",
        "value": "4056.412942708333"
      },
      {
        "field": "MemoryReserved",
        "value": "61440"
      },
      {
        "field": "MemoryUtilized",
        "value": "4311"
      },
      {
        "field": "@ptr",
        "value": "CpABClUKUTE0NjcxNzAzNzI0NzovYXdzL2Vjcy9jb250YWluZXJpbnNpZ2h0cy9pcmwtaW5mcmEtc2hhcmVkLWplbmtpbnMtbWFpbi9wZXJmb3JtYW5jZRABEjUaGAIGSaxosQAAAAAd+TFeAAZKKHIwAAABEiABKIDF9taRMTDA7v3WkTE4BkC9NEjNPFCBIRgAEAEYAQ=="
      }
    ]
  ]
}

我想在终端中显示的内容如下:

@timestamp                CpuReserved  CpuUtilized         MemoryReserved   MemoryUtilized  
==========================================================================================
2023-07-03 08:16:00.000   8192.0       410.5300065104166   61440            1417
2023-07-03 08:15:00.000   8192.0       702.310791015625    61440            792

有人可以引导我走向正确的方向吗?

答案1

或许:

$ jq -c '.results[]|map(.key=.field)|from_entries|del(."@ptr")' file.json |
   mlr --ijson --opprint --barred cat
+-------------------------+-------------+-------------------+----------------+----------------+
| @timestamp              | CpuReserved | CpuUtilized       | MemoryReserved | MemoryUtilized |
+-------------------------+-------------+-------------------+----------------+----------------+
| 2023-07-03 08:28:00.000 | 8192.0      | 4056.412942708333 | 61440          | 4311           |
| 2023-07-03 08:28:00.000 | 8192.0      | 4056.412942708333 | 61440          | 4311           |
+-------------------------+-------------+-------------------+----------------+----------------+

如果没有--barred,看起来像:

@̲t̲i̲m̲e̲s̲t̲a̲m̲p̲              C̲p̲u̲R̲e̲s̲e̲r̲v̲e̲d̲ C̲p̲u̲U̲t̲i̲l̲i̲z̲e̲d̲       M̲e̲m̲o̲r̲y̲R̲e̲s̲e̲r̲v̲e̲d̲ M̲e̲m̲o̲r̲y̲U̲t̲i̲l̲i̲z̲e̲d̲
2023-07-03 08:28:00.000 8192.0 4056.412942708333 61440 4311
2023-07-03 08:28:00.000 8192.0 4056.412942708333 61440 4311

jq您还可以将该命令的输出通过管道传输到vd -f json(可视数据) 代替mlr磨坊主)以获得交互式表格查看器。

它们用于jq提取信息并且mlr仅用于格式化表格。的功能集jq和 的功能集之间存在一些重叠mlr。例如,您还可以删除@ptr带有mlrs 的列cut

jq -c '.results[]|map(.key=.field)|from_entries' file.json |
  mlr --ijson --opprint cut -xf @ptr

jq命令被分解并评论:

jq -c '
  .results[] | # iterate over the elements of the .results array 
               # (which are also arrays)

  map(.key=.field) | # for each of those arrays, transform the
                     # elements (which are objects) by adding a 
                     # field of key "key" with same value as that
                     # with "field" key in each, as that's what
                     # from_entries needs

  from_entries | # transforms those [{"key":"foo","value":"bar"}]
                 # (the "field" field is ignored) to {"foo":"bar"}

  del(."@ptr") # deletes the field with key "@ptr" from those
               # objects' file.json

结果不是 JSON,而是多个 JSON 连接在一起,但两者都jq支持mlr。对于-c(compact),这就是 NDJSON(换行符分隔的 JSON),其中每行一个 JSON,也受vd.为了获得正确的 JSON,我们需要:

jq -c '.results|map(map(.key=.field)|from_entries|del(."@ptr"))' file.json

我们在数组map上使用.results它,因此它会生成另一个 JSON 数组,而不是迭代元素。所以最终的结果是一个大数组。这也得到了支持jq(显然这是正确的 JSON),mlr并且vd, 的输入时间有点长,意味着这些工具需要先读到]最后的结尾,然后才能进行任何操作。但在实践中,我并没有检查这是否会对性能产生任何影响。

答案2

完后还有:

$ jq -r '.results|map(map({key:.field,value}|select(.key!="@ptr"))|from_entries)|(.[0]|keys_unsorted) as $keys|([$keys]+map([.[$keys[]]]))[]|@csv' input.json | xsv table
@timestamp               CpuReserved  CpuUtilized        MemoryReserved  MemoryUtilized
2023-07-03 08:28:00.000  8192.0       4056.412942708333  61440           4311
2023-07-03 08:28:00.000  8192.0       4056.412942708333  61440           4311

笔记:

  • from_entries会将键值对数组转换为对象。
  • 要将对象数组转换为 csv:查看详情
  • xsv table仅用于美观打印,您可以将其删除。

答案3

我想我找到了答案:

jq -r '([ "@timestamp", "CpuReserved", "CpuUtilized", "MemoryReserved", "MemoryUtilized"] | (., map(length*"-"))), ( .results[] | [.[0,1,2,3,4].value] ) | @tsv' |column -ts $'\t'
@timestamp               CpuReserved  CpuUtilized         MemoryReserved  MemoryUtilized
----------               -----------  -----------         --------------  --------------
2023-07-03 08:28:00.000  8192.0       4056.412942708333   61440           4311
2023-07-03 08:28:00.000  8192.0       4056.412942708333   61440           4311
2023-07-03 08:27:00.000  8192.0       1056.0744270833331  61440           4436
2023-07-03 08:27:00.000  8192.0       1056.0744270833331  61440           4436
2023-07-03 08:26:00.000  8192.0       2756.6764583333334  61440           5138
2023-07-03 08:26:00.000  8192.0       2756.6764583333334  61440           5138
2023-07-03 08:25:00.000  8192.0       5715.494895833333   61440           6600
2023-07-03 08:25:00.000  8192.0       5715.494895833333   61440           6600
2023-07-03 08:24:00.000  8192.0       7977.704166666666   61440           8451
2023-07-03 08:24:00.000  8192.0       7977.704166666666   61440           8451
2023-07-03 08:23:00.000  8192.0       7288.051666666666   61440           4757
2023-07-03 08:23:00.000  8192.0       7288.051666666666   61440           4757
2023-07-03 08:22:00.000  8192.0       4286.02375          61440           4815
2023-07-03 08:22:00.000  8192.0       4286.02375          61440           4815
2023-07-03 08:21:00.000  8192.0       3357.523776041666   61440           2146
2023-07-03 08:21:00.000  8192.0       3357.523776041666   61440           2146
2023-07-03 08:20:00.000  8192.0       990.2647916666666   61440           1692
2023-07-03 08:20:00.000  8192.0       990.2647916666666   61440           1692
2023-07-03 08:19:00.000  8192.0       4533.409375         61440           1816
2023-07-03 08:19:00.000  8192.0       4533.409375         61440           1816
2023-07-03 08:18:00.000  8192.0       939.4855208333333   61440           1810
2023-07-03 08:18:00.000  8192.0       939.4855208333333   61440           1810
2023-07-03 08:17:00.000  8192.0       4770.659791666667   61440           1924
2023-07-03 08:17:00.000  8192.0       4770.659791666667   61440           1924
2023-07-03 08:16:00.000  8192.0       410.5300065104166   61440           1417
2023-07-03 08:16:00.000  8192.0       410.5300065104166   61440           1417
2023-07-03 08:15:00.000  8192.0       702.310791015625    61440           792
2023-07-03 08:15:00.000  8192.0       702.310791015625    61440           792
2023-07-03 08:14:00.000  8192.0       0.0                 61440           0
2023-07-03 08:14:00.000  8192.0       0.0                 61440           0

答案4

jq另一个用于发出制表符分隔值并column对其进行美化的方法

jq -r '
  .results
  | first as $first
  | [$first | map(.field)] + [.[] | map(.value)]
  | map(.[:-1])[]
  | @tsv
' file.json | column -t -s $'\t'
@timestamp               CpuReserved  CpuUtilized        MemoryReserved  MemoryUtilized
2023-07-03 08:28:00.000  8192.0       4056.412942708333  61440           4311
2023-07-03 08:28:00.000  8192.0       4056.412942708333  61440           4311

相关内容