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

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

Convert json to Markdown Table in bash

问题

Sure, here is the translated content:

  1. 我正在尝试将以下 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"
}

  1. 我尝试的命令:

cat json_file | jq -r '{"NAME": "ENDPOINT_ID"} + . | to_entries[] | "(.key)\t(.value)"'

  1. 我得到的输出:

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. 当以上输出提交到 README.md GitHub 文件时,会丢失缩进(空格),无法正确分为行和列,导致可视化效果不佳。
  2. 我参考的链接:

https://www.markdownguide.org/extended-syntax/#tables

  1. 预期的输出:
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.

  1. {
  2. "service_1":"abc-endpoint.vpce.amazonaws.com",
  3. "service_2":"def-endpoint.vpce.amazonaws.com",
  4. "service_3":"xyz-endpoint.vpce.amazonaws.com"
  5. }

Command I tried:

  1. cat json_file | jq -r '{"NAME": "ENDPOINT_ID"} + . | to_entries[] | "\(.key)\t\(.value)"'

Output I am getting:

  1. NAME ENDPOINT-ID
  2. service_1 abc-endpoint.vpce.amazonaws.com
  3. service_2 def-endpoint.vpce.amazonaws.com
  4. 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:

  1. https://www.markdownguide.org/extended-syntax/#tables

Expected output:

  1. | NAME | ENDPOINT_ID |
  2. | -------- | -------- |
  3. | service_1 | abc-endpoint.vpce.amazonaws.com |
  4. | service_2 | def-endpoint.vpce.amazonaws.com |
  5. | service_3 | xyz-endpoint.vpce.amazonaws.com |

答案1

得分: 2

A semi hard-coded solution could look like:

  1. |名称|终端点ID|
  2. |------|------|
  3. (to_entries | map("|\(key)|\(value)|") | join("\n"))
  1. 硬编码的标题 + 第二行
  2. 循环(map())使用 to_entries 来使用 keyvalue
    1. 创建一个字符串,将 key 和 value 包围在 |
    2. 使用换行符 (\n) 来 join() 这些行

上述代码将输出:

  1. |名称|终端点ID|
  2. |------|------|
  3. |service_1|abc-endpoint.vpce.amazonaws.com|
  4. |service_2|def-endpoint.vpce.amazonaws.com|
  5. |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:

  1. "|NAME|ENDPOINT_ID|\n|------|------|\n" +
  2. (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:


  1. |NAME|ENDPOINT_ID|
  2. |------|------|
  3. |service_1|abc-endpoint.vpce.amazonaws.com|
  4. |service_2|def-endpoint.vpce.amazonaws.com|
  5. |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:

  1. % jq -r '{"NAME": "ENDPOINT_ID"} + {"---": "---"} + . | to_entries[] | "|\(.key)|\(.value)|"'<<-eof 55ms
  2. {
  3. "service_1":"abc-endpoint.vpce.amazonaws.com",
  4. "service_2":"def-endpoint.vpce.amazonaws.com",
  5. "service_3":"xyz-endpoint.vpce.amazonaws.com"
  6. }
  7. eof

Gives me:

  1. |NAME|ENDPOINT_ID|
  2. |---|---|
  3. |service_1|abc-endpoint.vpce.amazonaws.com|
  4. |service_2|def-endpoint.vpce.amazonaws.com|
  5. |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:

  1. % jq -r '{"NAME": "ENDPOINT_ID"} + {"---": "---"} + . | to_entries[] | "|\(.key)|\(.value)|"'<<-eof 55ms
  2. {
  3. "service_1":"abc-endpoint.vpce.amazonaws.com",
  4. "service_2":"def-endpoint.vpce.amazonaws.com",
  5. "service_3":"xyz-endpoint.vpce.amazonaws.com"
  6. }
  7. eof

Gives me:

  1. |NAME|ENDPOINT_ID|
  2. |---|---|
  3. |service_1|abc-endpoint.vpce.amazonaws.com|
  4. |service_2|def-endpoint.vpce.amazonaws.com|
  5. |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工具显示结果:

  1. $ cat json_file | jq -r '{ "NAME": "ENDPOINT_ID" } + . | to_entries[] | [.key, .value] | @csv' | csview -s Markdown
  2. | NAME | ENDPOINT_ID |
  3. |-----------|---------------------------------|
  4. | service_1 | abc-endpoint.vpce.amazonaws.com |
  5. | service_2 | def-endpoint.vpce.amazonaws.com |
  6. | service_3 | xyz-endpoint.vpce.amazonaws.com |
  1. <details>
  2. <summary>英文:</summary>
  3. convert **jq** output to *csv* and then display result with [csview][1] utility
  4. $ cat json_file |jq -r &#39;{&quot;NAME&quot;: &quot;ENDPOINT_ID&quot;} + . |to_entries[] | [.key, .value] | @csv&#39; |csview -s Markdown
  5. | NAME | ENDPOINT_ID |
  6. |-----------|---------------------------------|
  7. | service_1 | abc-endpoint.vpce.amazonaws.com |
  8. | service_2 | def-endpoint.vpce.amazonaws.com |
  9. | service_3 | xyz-endpoint.vpce.amazonaws.com |
  10. [1]: https://github.com/wfxr/csview
  11. </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:

确定