json 将jq输出格式化为表格

vq8itlhq  于 2023-02-01  发布在  其他
关注(0)|答案(3)|浏览(153)

我想从下面的JSON代码中获取一些数据:
我可以使用下面的命令得到输出,但现在我想格式化它,使我的输出看起来像预期的输出。

    • 命令:**
cat dump | jq -r '["name","IP","NAT","location","method"], 
                  (.objects[] | [.name, ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."install-on", ."nat-settings".method]) 
                              | @csv' 
                              | sed -e 's/"//g'
    • 使用@csv后,我得到以下输出:**
name,IP,NAT,location,method
H_103.109.135.25,103.109.135.25,1.1.1.1,All,static
H_103.109.135.250,103.109.135.250,,,

每当我使用@tsv时,我得到**"jq:错误:tsv不是有效格式"**
有谁能建议我如何实现以下输出:

    • 预期产出:**

    • 原始JSON代码:**
{
  "from" : 1,
  "to" : 2,
  "total" : 2,
  "objects" : [ {
    "uid" : "73b7036d-e8ec-47b7-99b5-19ca89eb5fd0",
    "name" : "H_103.109.135.25",
    "type" : "host",
    "domain" : {
      "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
      "name" : "SMC User",
      "domain-type" : "domain"
    },
    "ipv4-address" : "103.109.135.25",
    "interfaces" : [ ],
    "nat-settings" : {
      "auto-rule" : true,
      "ipv4-address" : "1.1.1.1",
      "ipv6-address" : "",
      "install-on" : "All",
      "method" : "static"
    },
    "comments" : "",
    "color" : "black",
    "icon" : "Objects/host",
    "tags" : [ ],
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1674820459413,
        "iso-8601" : "2023-01-27T17:24+0530"
      },
      "last-modifier" : "admin",
      "creation-time" : {
        "posix" : 1674818326777,
        "iso-8601" : "2023-01-27T16:48+0530"
      },
      "creator" : "admin"
    },
    "read-only" : false,
    "available-actions" : {
      "edit" : "true",
      "delete" : "true",
      "clone" : "true"
    }
  }, {
    "uid" : "7300c38a-a496-497a-b9e3-5701fa081393",
    "name" : "H_103.109.135.250",
    "type" : "host",
    "domain" : {
      "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
      "name" : "SMC User",
      "domain-type" : "domain"
    },
    "ipv4-address" : "103.109.135.250",
    "interfaces" : [ ],
    "nat-settings" : {
      "auto-rule" : false
    },
    "comments" : "",
    "color" : "black",
    "icon" : "Objects/host",
    "tags" : [ ],
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1674818341888,
        "iso-8601" : "2023-01-27T16:49+0530"
      },
      "last-modifier" : "admin",
      "creation-time" : {
        "posix" : 1674818341888,
        "iso-8601" : "2023-01-27T16:49+0530"
      },
      "creator" : "admin"
    },
    "read-only" : false,
    "available-actions" : {
      "edit" : "true",
      "delete" : "true",
      "clone" : "true"
    }
  } ]
}
    • 注:**

只使用jq将输出打印在表格中并不是强制性的。"awk"或"sed"也可以。
我已经从以下原始json数据中提取了所需的数据:

    • 提取数据:**
{
    "name": "H_103.109.135.25",
    "IP": "103.109.135.25",
    "NAT": "1.1.1.1",
    "location": "All",
    "method": "static"
  },
  {
    "name": "H_103.109.135.250",
    "IP": "103.109.135.250",
    "NAT": "NA",
    "location": "NA",
    "method": "NA"
  }

我现在只需要将此数据格式化为如下表或类似的格式:

| name              | IP              | NAT     | location   | method   |
|-------------------|-----------------|---------|------------|----------|
| H_103.109.135.25  | 103.109.135.25  | 1.1.1.1 | All        | static   |
| H_103.109.135.250 | 103.109.135.250 | NA      | NA         | NA       |
czq61nw1

czq61nw11#

jbtl可能会产生你想要的结果。例如,如果你在output.jq中有这样的结果:

.objects
| map(
    { name, IP: ."ipv4-address" } +
    (."nat-settings" | {
      NAT: (."ipv4-address" // "NA"), 
      location: (."install-on" // "NA"), 
      method: (.method // "NA")
    })
  )

然后通过这个过滤器传递数据,并使用-m选项将其传输到jtbl,如下所示:

cat dump | jq -f output.jq | jtbl -m

给出了这个

| name              | IP              | NAT     | location   | method   |
|-------------------|-----------------|---------|------------|----------|
| H_103.109.135.25  | 103.109.135.25  | 1.1.1.1 | All        | static   |
| H_103.109.135.250 | 103.109.135.250 | NA      | NA         | NA       |
x8diyxa7

x8diyxa72#

miller对于打印输出非常方便。

echo 'name,IP,NAT,location,method
H_103.109.135.25,103.109.135.25,1.1.1.1,All,static
H_103.109.135.250,103.109.135.250,,,' \
| mlr --c2p --barred put 'for (i,v in $*) {if (v == "") {$[i] = "NA"}}'

--c2p--icsv --opprint的快捷方式,它读取CSV输入并输出打印精美的表格形式。

+-------------------+-----------------+---------+----------+--------+
| name              | IP              | NAT     | location | method |
+-------------------+-----------------+---------+----------+--------+
| H_103.109.135.25  | 103.109.135.25  | 1.1.1.1 | All      | static |
| H_103.109.135.250 | 103.109.135.250 | NA      | NA       | NA     |
+-------------------+-----------------+---------+----------+--------+

miller put动词的脚本很笨拙。
参见https://miller.readthedocs.io/en/latest/
更实用的款式:

mlr --c2p --barred put '$* = apply($*, func(k,v) {return {k: v == "" ? "NA" : v}})'

我建议在jq中去掉引号并添加"NA",然后将输出通过管道传输到column

jq -r '
  [
    ["name","IP","NAT","location","method"],
    ( .objects[]
    | {"nat-settings": {"ipv4-address": "NA", "install-on": "NA", method: "NA"}} * .
    | [.name, ."ipv4-address"] + (."nat-settings" | [."ipv4-address", ."install-on", .method])
    )
  ][] | join(",")
' dump | column -s, -t

假设"nat-settings"对象"缺少" ipv4-address "等键。

raogr8fs

raogr8fs3#

我建议使用jq的@tsv和非常标准的工具column,例如:

< dump jq -r '
  ["name","IP","NAT","location","method"], 
  (.objects[] | [.name, ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."install-on", ."nat-settings".method]) 
  | @tsv' | column -t

相关问题