skip to Main Content

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   |

3

Answers


  1. 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

    Login or Signup to reply.
  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:
    enter image description here

    Login or Signup to reply.
  3. convert jq output to csv and then display result with csview 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 |
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search