Convert json to Markdown Table in bash 将json转换为Markdown表格在bash中

huangapple go评论146阅读模式
英文:

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"))
  1. 硬编码的标题 + 第二行
  2. 循环(map())使用 to_entries 来使用 keyvalue
    1. 创建一个字符串,将 key 和 value 包围在 |
    2. 使用换行符 (\n) 来 join() 这些行

上述代码将输出:

|名称|终端点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"))
  1. Hard-coded header + second line
  2. Loop (map()) over to_entires to use key and value
    1. Create a string were we surround key and value in |
    2. join() those lines with a newline (\n)

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:
Convert json to Markdown Table in bash
将json转换为Markdown表格在bash中"

希望这对您有所帮助。

英文:

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:
Convert json to Markdown Table in bash
将json转换为Markdown表格在bash中

答案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 &#39;{&quot;NAME&quot;: &quot;ENDPOINT_ID&quot;} + . |to_entries[] | [.key, .value] | @csv&#39; |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>



huangapple
  • 本文由 发表于 2023年4月17日 23:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定