GitHub Actions - GitHub JSON output between jobs

by
Jeremy Canfield |
Updated: April 15 2024
| GitHub Actions articles
GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.
This also assumes you are familiar with Output. If not, check out my article GitHub Actions - Getting Started with Output.
You can
- Pass output between jobs
- Pass JSON output between jobs (this article)
- Pass input between workflows
Here is an example of how you can create JSON in job 1 and then used the JSON in job 2.
name: GitHub Action
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
push:
branches:
- main
jobs:
job1:
runs-on: ubuntu-latest
steps:
- id: set-matrix
run: echo "matrix={\"include\":[{\"project\":\"foo\",\"hello\":\"World\"},{\"project\":\"bar\",\"hello\":\"World\"}]}" >> $GITHUB_OUTPUT
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
job2:
runs-on: ubuntu-latest
needs: job1
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.job1.outputs.matrix) }}
steps:
# This should return: {project: foo, hello: World} and {project: bar, hello: World}
- name: full JSON
run: |
echo "result = ${{ toJSON(matrix) }}"
# This should return foo and bar
- name: JSON project
run: |
echo "result = ${{ toJSON(matrix.project) }}"
# This should return World
- name: JSON foo
run: |
echo "result = ${{ toJSON(matrix.foo) }}"
Did you find this article helpful?
If so, consider buying me a coffee over at