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().

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"
    ]