Output JSON string from one action and use it in a workflow step

At the moment it looks like output from actions (set via core.setOutput(‘variable’, value)) can only be simple string values.

I would really like to be able to set output values to be JSON that can then be used in workflow expressions. Althought there is a toJSON function for use in workflow expressions there is no corresponding way to parse JSON from action outputs.

Example of what I’d like:

test:
    steps:
    # An action that writes a JSON object (as a string) into an output 'foo'
    - uses: custom-action/action1@v1
      id: action1
    - uses: custom-action/action2@v1
      with:
        input: ${{ parseJSON(steps.action1.output.foo).*.name }}

At the moment the only way to pass structured data between actions is via strings and then parse the string in the subsequent action.

1 Like

You could not get the json propery value as an action’s input directly.  

I found a way to set Json string as output variable value. You need to add ‘’ to both end of your json string.

And you could use jq method to get json properties. 

Please see my example: 

steps:
    - name: set output
      id: set
      run: |
         echo ::set-output name=json_var::'[{ "name": "test", "client_payload": "111" }, { "name": "test2", "client_payload": "222" }] '
    - name: use output
      run: |
        echo $json_var | jq '.[].name'
      env:
        json_var: ${{ steps.set.outputs.json_var}}

If you don’t want to use jq in bash scripts, you could pass the json string directly as the input of your custom action and deal with it inside your action.  

2 Likes

Should I install jq to use as you mentioned? thanks

No. Just run those steps on ubuntu-latest or macos-latest . If your job runs on windows machine, you could specify shell : bash for   jobs.<job_id>.steps.run

on:
  push:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: set output
      id: set
      run: |
         echo ::set-output name=json_var::'[{ "name": "test", "client_payload": "111" }, { "name": "test2", "client_payload": "222" }] '
    - name: use output
      run: |
        echo $json_var | jq '.[].name'
      env:
        json_var: ${{ steps.set.outputs.json_var}}
1 Like