Search code examples
bashtext-processinggithub

convert list of word in github actions into json array and use as a strategy matrix


I have in github actions a list of words with spaces in the shell Bash. e.g.

hello1 hello2 hello3

The goal is to convert this list to a JSON array format, write it to the output variable and use it as a strategy matrix via fromJSON().

  • I can already successfully write the variable as GITHUB_OUTPUT.
  • What is missing is to convert the upper list into a valid json array
    ['hello1','hello2','hello3']
    
    that can be used in the next job in the strategy matrix.

Thank you for your help.


Solution

  • You could use jq - either treating standard input as raw input

    $ echo hello1 hello2 hello3 | jq --raw-input 'split(" ")'
    [
      "hello1",
      "hello2",
      "hello3"
    ]
    

    or passing the string as a variable

    $ var='hello1 hello2 hello3'
    $ jq --null-input --arg str "$var" '$str | split(" ")'
    [
      "hello1",
      "hello2",
      "hello3"
    ]