[英]format jq output into a table
I want to fetch some data from below JSON code:我想从下面的 JSON 代码中获取一些数据:
I'm able to get the output using below command but now I want to format it in such a way that my output will look like the expected output.我可以使用下面的命令获得 output,但现在我想以这样的方式格式化它,使我的 output 看起来像预期的 output。
Command:命令:
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'
After using @csv I got below output:使用@csv 后,我得到了以下 output:
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,,,
and whenever I use @tsv I get "jq: error: tsv is not a valid format"每当我使用@tsv 时,我都会收到“jq: error: tsv is not a valid format”
Can any one suggest me how can I achieve below output:任何人都可以建议我如何实现低于 output 的目标:
Expected Output:预计 Output:
Raw JSON Code:原始 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"
}
} ]
}
Note:笔记:
It's not mandatory that the output should be printed in table using jq only.仅使用 jq 将 output 打印在表格中并不是强制性的。 "awk" or "sed" is also fine.
“awk”或“sed”也可以。
I have extracted data that required from the below raw json data:我从以下原始 json 数据中提取了所需的数据:
Extracted data:提取的数据:
{
"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"
}
I now just need to format this data into table like below or somewhat similar:我现在只需要将这些数据格式化成如下表或类似的表:
| 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 |
There is jbtl which may produce what you're looking for.有jbtl可以产生你正在寻找的东西。 If you have this in
output.jq
for example:例如,如果您在
output.jq
中有这个:
.objects
| map(
{ name, IP: ."ipv4-address" } +
(."nat-settings" | {
NAT: (."ipv4-address" // "NA"),
location: (."install-on" // "NA"),
method: (.method // "NA")
})
)
then passing the data through this filter and piping it into jtbl
with the -m
option, like this:然后通过此过滤器传递数据并使用
-m
选项将其通过管道传输到jtbl
,如下所示:
cat dump | jq -f output.jq | jtbl -m
gives this给这个
| 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 is handy for pretty-printing output. miller可以方便地进行漂亮的打印 output。
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
is a shortcut for --icsv --opprint
which reads CSV input and outputs pretty-printed tabular form. --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 |
+-------------------+-----------------+---------+----------+--------+
The miller put
verb takes an awk-like script. miller
put
动词采用类似 awk 的脚本。
See https://miller.readthedocs.io/en/latest/参见https://miller.readthedocs.io/en/latest/
A bit more functional style:更实用的风格:
mlr --c2p --barred put '$* = apply($*, func(k,v) {return {k: v == "" ? "NA" : v}})'
I'd suggest removing quotes and adding "NA" inside jq, and then pipe the output to column
我建议删除引号并在 jq 中添加“NA”,然后将 pipe 和 output 添加到
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
That assumes that the "nat-settings" object is missing the "ipv4-address", etc, keys.假设“nat-settings”object缺少“ipv4-address”等键。
I would recommend using jq's @tsv
and the very standard tool, column
, eg as follows:我建议使用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.