英文:
Convert json to Markdown Table in bash
问题
Sure, here is the translated content:
我正在尝试将以下 JSON 转换为 Markdown 表格(GitHub 文档(.md 文件)),但无法成功。
{
"service_1":"abc-endpoint.vpce.amazonaws.com",
"service_2":"def-endpoint.vpce.amazonaws.com",
"service_3":"xyz-endpoint.vpce.amazonaws.com"
}
我尝试的命令:
cat json_file | jq -r '{"NAME": "ENDPOINT_ID"} + . | to_entries[] | "(.key)\t(.value)"'
我得到的输出:
NAME ENDPOINT-ID
service_1 abc-endpoint.vpce.amazonaws.com
service_2 def-endpoint.vpce.amazonaws.com
service_3 xyz-endpoint.vpce.amazonaws.com
当以上输出提交到 README.md GitHub 文件时,会丢失缩进(空格),无法正确分为行和列,导致可视化效果不佳。
我参考的链接:
https://www.markdownguide.org/extended-syntax/#tables
预期的输出:
NAME | ENDPOINT_ID |
---|---|
service_1 | abc-endpoint.vpce.amazonaws.com |
service_2 | def-endpoint.vpce.amazonaws.com |
service_3 | xyz-endpoint.vpce.amazonaws.com |
英文:
I am trying to convert the below json to a markdown table(GitHub docs(.md file)), however not able to do so.
{
"service_1":"abc-endpoint.vpce.amazonaws.com",
"service_2":"def-endpoint.vpce.amazonaws.com",
"service_3":"xyz-endpoint.vpce.amazonaws.com"
}
Command I tried:
cat json_file | jq -r '{"NAME": "ENDPOINT_ID"} + . | to_entries[] | "\(.key)\t\(.value)"'
Output I am getting:
NAME ENDPOINT-ID
service_1 abc-endpoint.vpce.amazonaws.com
service_2 def-endpoint.vpce.amazonaws.com
service_3 xyz-endpoint.vpce.amazonaws.com
The above output when committed to README.md github file losses its indentation(spaces) and is not properly divided into rows and columns, leading to bad visualization.
Link that I went through:
https://www.markdownguide.org/extended-syntax/#tables
Expected output:
| NAME | ENDPOINT_ID |
| -------- | -------- |
| service_1 | abc-endpoint.vpce.amazonaws.com |
| service_2 | def-endpoint.vpce.amazonaws.com |
| service_3 | xyz-endpoint.vpce.amazonaws.com |
答案1
得分: 2
A semi hard-coded solution could look like:
|名称|终端点ID|
|------|------|
(to_entries | map("|\(key)|\(value)|") | join("\n"))
- 硬编码的标题 + 第二行
- 循环(
map()
)使用to_entries
来使用key
和value
- 创建一个字符串,将 key 和 value 包围在
|
中 - 使用换行符 (
\n
) 来join()
这些行
- 创建一个字符串,将 key 和 value 包围在
上述代码将输出:
|名称|终端点ID|
|------|------|
|service_1|abc-endpoint.vpce.amazonaws.com|
|service_2|def-endpoint.vpce.amazonaws.com|
|service_3|xyz-endpoint.vpce.amazonaws.com|
渲染为
名称 | 终端点ID |
---|---|
service_1 | abc-endpoint.vpce.amazonaws.com |
service_2 | def-endpoint.vpce.amazonaws.com |
service_3 | xyz-endpoint.vpce.amazonaws.com |
JqPlay演示
英文:
A semi hard-coded solution could look like:
"|NAME|ENDPOINT_ID|\n|------|------|\n" +
(to_entries | map("|\(.key)|\(.value)|") | join("\n"))
- Hard-coded header + second line
- Loop (
map()
) overto_entires
to usekey
andvalue
- Create a string were we surround key and value in
|
join()
those lines with a newline (\n
)
- Create a string were we surround key and value in
The above will output:
|NAME|ENDPOINT_ID|
|------|------|
|service_1|abc-endpoint.vpce.amazonaws.com|
|service_2|def-endpoint.vpce.amazonaws.com|
|service_3|xyz-endpoint.vpce.amazonaws.com|
Which renders as
NAME | ENDPOINT_ID |
---|---|
service_1 | abc-endpoint.vpce.amazonaws.com |
service_2 | def-endpoint.vpce.amazonaws.com |
service_3 | xyz-endpoint.vpce.amazonaws.com |
JqPlay Demo
答案2
得分: 2
这是您要翻译的部分:
"Very slightly less hard coded than @0stone0's answer:
% jq -r '{"NAME": "ENDPOINT_ID"} + {"---": "---"} + . | to_entries[] | "|\(.key)|\(.value)|"'<<-eof 55ms
{
"service_1":"abc-endpoint.vpce.amazonaws.com",
"service_2":"def-endpoint.vpce.amazonaws.com",
"service_3":"xyz-endpoint.vpce.amazonaws.com"
}
eof
Gives me:
|NAME|ENDPOINT_ID|
|---|---|
|service_1|abc-endpoint.vpce.amazonaws.com|
|service_2|def-endpoint.vpce.amazonaws.com|
|service_3|xyz-endpoint.vpce.amazonaws.com|
Looks right in github preview:
"
希望这对您有所帮助。
英文:
Very slightly less hard coded than @0stone0's answer:
% jq -r '{"NAME": "ENDPOINT_ID"} + {"---": "---"} + . | to_entries[] | "|\(.key)|\(.value)|"'<<-eof 55ms
{
"service_1":"abc-endpoint.vpce.amazonaws.com",
"service_2":"def-endpoint.vpce.amazonaws.com",
"service_3":"xyz-endpoint.vpce.amazonaws.com"
}
eof
Gives me:
|NAME|ENDPOINT_ID|
|---|---|
|service_1|abc-endpoint.vpce.amazonaws.com|
|service_2|def-endpoint.vpce.amazonaws.com|
|service_3|xyz-endpoint.vpce.amazonaws.com|
答案3
得分: 0
将jq的输出转换为csv,然后使用csview工具显示结果:
$ cat json_file | jq -r '{ "NAME": "ENDPOINT_ID" } + . | to_entries[] | [.key, .value] | @csv' | csview -s Markdown
| NAME | ENDPOINT_ID |
|-----------|---------------------------------|
| service_1 | abc-endpoint.vpce.amazonaws.com |
| service_2 | def-endpoint.vpce.amazonaws.com |
| service_3 | xyz-endpoint.vpce.amazonaws.com |
<details>
<summary>英文:</summary>
convert **jq** output to *csv* and then display result with [csview][1] utility
$ cat json_file |jq -r '{"NAME": "ENDPOINT_ID"} + . |to_entries[] | [.key, .value] | @csv' |csview -s Markdown
| NAME | ENDPOINT_ID |
|-----------|---------------------------------|
| service_1 | abc-endpoint.vpce.amazonaws.com |
| service_2 | def-endpoint.vpce.amazonaws.com |
| service_3 | xyz-endpoint.vpce.amazonaws.com |
[1]: https://github.com/wfxr/csview
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论