GitHub Actions "You have an error in your yaml syntax" for a dynamic matrix in CI
A dynamic matrix built its JSON with string interpolation and produced malformed content: unquoted keys, single quotes, a trailing comma, or embedded newlines. GitHub or fromJSON then reports a yaml/JSON syntax error.
What this error means
The generated matrix fails to parse with "You have an error in your yaml syntax" or a JSON parse error, typically when the producing step assembled the string by hand.
Error: You have an error in your yaml syntax
while parsing a flow node
did not find expected node contentCommon causes
Hand-built JSON with wrong quoting
Concatenating strings often produces single-quoted keys, missing quotes, or a trailing comma, none of which are valid JSON for fromJSON.
Newlines or shell expansion leaked into the value
A multi-line output or an unescaped variable injected a newline, so the value spanning $GITHUB_OUTPUT lines breaks parsing.
How to fix it
Generate JSON with a real tool
- Build the array or object with
jq(or your language) instead of string concatenation. - Emit compact output with
jq -cso it stays on one line. - Write it to
$GITHUB_OUTPUTand consume it viafromJSON.
matrix=$(jq -cn '{include: [{os:"ubuntu-latest"},{os:"macos-latest"}]}')
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"Validate the JSON before using it
Pipe the generated value through jq . in the same step so a malformed matrix fails there with a clear message, not later in strategy evaluation.
echo "$matrix" | jq . > /dev/nullHow to prevent it
- Never hand-concatenate matrix JSON; use jq or a script.
- Emit single-line compact JSON to $GITHUB_OUTPUT.
- Validate the value with jq before it reaches fromJSON.