Search code examples
yq

Simple yq (go) to join key values and output to markdown


I am trying to use the go version of yq to produce markdown from a json array.

Given the following json array:

[
    { "en": "cat", "fr": "chat" },
    { "en": "dog", "fr": "chien" },
    { "en": "fish", "fr": "poisson" }
]

I want to convert to markdown something like:

> cat
 - chat
> dog
 - chien
> fish
 - poisson

I have tried various pipes but all I can come up with does not produce the desired outcome

yq -r '"> " + .[].en, .[].fr'  test.json 
[WARNING] 10:37:36 JSON file output is now JSON by default (instead of yaml). Use '-oy' or '--output-format=yaml' for yaml output
> cat
> dog
> fish
chat
chien
poisson

Solution

  • You are using .[] twice, and each (sequentially run) iteration just extracts one of the parts needed. Iterate once, and extract both values in one go.

    Using jqlang/jq or kislyuk/yq:

    .[] | "> " + .en, " - " + .fr
    

    For mikefarah/yq, make it one filter by concatenating with \n to keep the (iterated) context:

    .[] | "> " + .en + "\n - " + .fr
    

    Result:

    > cat
     - chat
    > dog
     - chien
    > fish
     - poisson