FIX: GitHub Actions – Unable to process file command ‘output’ successfully JSON

How to add Json to GitHub actions output

I had a fun one today at work. I am fairly new with GitHub workflows & actions. The best part about it is that I can use PowerShell for most actions.

In one of the GitHub actions, I need to output the customerConfig to GitHub output as JSON.

Sounds easy, but unfortunately, I got this error:

Error: Unable to process file command 'output' successfully.
Error: Invalid format '  "id": "",'
FIX: GitHub Actions - Unable to process file command 'output' successfully JSON
FIX: GitHub Actions – Unable to process file command ‘output’ successfully JSON

It didn’t take too long to figure it out, but it’s worth a blog post.


Why do I get an “Error: Unable to process file command ‘output’ successfully – invalid format“?

This error happens because GitHub Actions expects JSON output in a single-line format, but PowerShell’s ConvertTo-Json command pretty-prints the output by default.

That means it adds white spaces and newlines, which GitHub Actions doesn’t like (and I suspect it’s mostly the newlines it cannot deal with).

If you’re using a command like this in PowerShell:

$Object = @{
    My = 'Name'
    Is = 'Bas'
}

$Object | ConvertTo-Json

{
  "My": "Name",
  "Is": "Bas"
}

This command formats JSON across multiple lines, causing GitHub Actions to break when trying to read it.


Let’s stringify or in PowerShell ‘terms’ -Compress it

To fix this in PowerShell, you just need to add the -Compress parameter to ConvertTo-Json. So I will show you the example below in PowerShell, but developers mostly call this stringify.

$Object = @{
    My = 'Name'
    Is = 'Bas'
}

$Object | ConvertTo-Json -Compress

{"My":"Name","Is":"Bas"}

This removes unnecessary spaces and newlines, ensuring GitHub Actions can read the JSON correctly.

Published by

Bas Wijdenes

My name is Bas Wijdenes and I work as a PowerShell DevOps Engineer. In my spare time I write about interesting stuff that I encounter during my work.

Leave a Reply

Your email address will not be published. Required fields are marked *