Skip to main content

You could also pipe the output of that jq command to vd -f json (visidataVisiData) instead of mlr (Miller) to get an interactive table viewer.

You could also pipe the output of that jq command to vd -f json (visidata) instead of mlr to get an interactive table viewer.

You could also pipe the output of that jq command to vd -f json (VisiData) instead of mlr (Miller) to get an interactive table viewer.

added 1541 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

That jq command, broken down and commented:

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 

The result is not JSON, but several JSONs concatenated together, but both jq and mlr support that. With -c (compact), that's NDJSON (newline-delimited JSON) where we have one JSON per line, also supported by vd. To get proper JSON, we'd need:

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

Where we use map on the .results array so it results in another JSON array instead of iterating over the elements. So the end result is one large arrays. That's also supported by jq (obviously as that's proper JSON), mlr and vd, is a bit longer to type and means those tools need to read up to the closing ] at the very end before they have anything to chew on. In practice, I've not checked whether that made any difference in terms of performance though.

That jq command, broken down and commented:

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 

The result is not JSON, but several JSONs concatenated together, but both jq and mlr support that. With -c (compact), that's NDJSON (newline-delimited JSON) where we have one JSON per line, also supported by vd. To get proper JSON, we'd need:

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

Where we use map on the .results array so it results in another JSON array instead of iterating over the elements. So the end result is one large arrays. That's also supported by jq (obviously as that's proper JSON), mlr and vd, is a bit longer to type and means those tools need to read up to the closing ] at the very end before they have anything to chew on. In practice, I've not checked whether that made any difference in terms of performance though.

added 4 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
$ jq -c 'map('.[]|mapresults[]|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 | +-------------------------+-------------+-------------------+----------------+----------------+ 
 @̱ṯi̱m̱e̱s̱ṯa̱m̱p̱@̲t̲i̲m̲e̲s̲t̲a̲m̲p̲ C̱p̱u̱Ṟe̱s̱e̱ṟv̱e̱ḏC̲p̲u̲R̲e̲s̲e̲r̲v̲e̲d̲ C̱p̱u̱U̱ṯi̱ḻi̱ẕe̱ḏC̲p̲u̲U̲t̲i̲l̲i̲z̲e̲d̲ M̱e̱m̱o̱ṟy̱Ṟe̱s̱e̱ṟv̱e̱ḏM̲e̲m̲o̲r̲y̲R̲e̲s̲e̲r̲v̲e̲d̲ M̱e̱m̱o̱ṟy̱U̱ṯi̱ḻi̱ẕe̱ḏ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 -c 'map('.[]|mapresults[]|map(.key=.field)|from_entries)'|from_entries' file.json | mlr --ijson --opprint cut -xf @ptr 
$ jq -c 'map(.[]|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 | +-------------------------+-------------+-------------------+----------------+----------------+ 
 @̱ṯi̱m̱e̱s̱ṯa̱m̱p̱ C̱p̱u̱Ṟe̱s̱e̱ṟv̱e̱ḏ C̱p̱u̱U̱ṯi̱ḻi̱ẕe̱ḏ M̱e̱m̱o̱ṟy̱Ṟe̱s̱e̱ṟv̱e̱ḏ M̱e̱m̱o̱ṟy̱U̱ṯi̱ḻi̱ẕe̱ḏ 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 -c 'map(.[]|map(.key=.field)|from_entries)' file.json | mlr --ijson --opprint cut -xf @ptr 
$ 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 | +-------------------------+-------------+-------------------+----------------+----------------+ 
 @̲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 -c '.results[]|map(.key=.field)|from_entries' file.json | mlr --ijson --opprint cut -xf @ptr 
added 1 character in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 337 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 134 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 3 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 372 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 372 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading