What do `toJSON` and `fromJSON` do, and give a real use of each?
IntermediateConceptContexts, Expressions, Env & Outputs
Answer
toJSON(value) serializes a context to pretty JSON: run: echo '${{ toJSON(github.event) }}' for debugging (dump through env to avoid quoting issues). fromJSON(string) parses JSON into an object, array, boolean or number:
strategy:
matrix: ${{ fromJSON(needs.plan.outputs.matrix) }}
timeout-minutes: ${{ fromJSON(vars.DEPLOY_TIMEOUT) }}
if: fromJSON(inputs.run_e2e)
The second one is how you get a number from a string variable where a number is required.
More on Contexts, Expressions, Env & Outputs
List the available contexts and what each holds.BeginnerWhich contexts can you use in which keys? Give two examples that trip people up.AdvancedWhat are the expression operators and how do comparisons coerce types?IntermediateHow do you write a ternary in GitHub Actions expressions?IntermediateWhat do `contains`, `startsWith`, `endsWith`, `format` and `join` do?BeginnerWhat are object filters (`.*`) and how are they useful?Intermediate