winget-cli
winget-cli copied to clipboard
Add list option to format output as json
Description of the new feature / enhancement
The list command outputs a human readable table of installed packages that is not easy to parse by an external tool. Formatting the list output as JSON by adding a --JsonOutput command line option would be very useful.
Proposed technical implementation details
No response
We've discussed having "--json" as an argument to do exactly this, but hadn't created an issue for it :)
I am playing with it right now and thought how good it would be to have the --json
parameter.
For someone having the same problem right now -> Here is a nice Powershell script which can parse the output to a custom object. Just be aware that the table heders are important and they are OS language-dependent 😄
It's strange a --as-yaml
option wasn't baked into winget search
and winget show
from day one. Just spit out the YAML as you get it. The existing system almost seems designed to make parsing impossible. How much work would be needed to implement either YAML or JSON output?
@WillPittenger we've been open source since the very first preview in 2020. There has been a mixture of priorities, and the overall roadmap philosophy is shared. This simply wasn't something the community wanted as a top priority.
Looking at the top-most 👍Issues:
-
#182 is a prerequisite to:
-
#140 followed by:
-
#476 and then:
-
#221
Supporting PowerShell as a native interface will expose rich objects for many of the scripted/automated scenarios being asked for by many IT Professionals, as well as many developers and power users. We've been considering exposing the CLI output as JSON as an additional value add since most of the work would have to be done in order to support a native PowerShell experience.
I agree 100% with PowerShell support! The reason JSON output was requested is because it is readily consumed by PowerShell.
JSON output would be much easier for our projects. Scripts would become much simpler 😇
Not sure if everyone is aware of this already or not, but for those that are looking to get winget results in JSON format, I was looking for the same and came upon the winget.run REST API which serves my purposes fine for now until winget itself is able to provide this functionality.
For my own use case, I only needed JSON for search results rather than for installed packages, so in my case just directly using the API alone satisfies my needs, but it could also potentially be useful for those looking to get JSON for installed packages as well if you're willing to jump through an additional hoop. The API doesn't know what packages you have installed, but if you use the winget cli itself to get just a list of your installed package ids, you can then pass those ids directly into the winget.run API which will get you the JSON for all of your installed packages.
Not ideal, but perhaps this may be helpful for some others until the eventual time when the winget cli will inevitably satisfy all of these use cases itself once further development is completed.
https://docs.winget.run/docs/introduction/
https://github.com/winget-run/wingetdotrun
EDIT: Oops, someone pointed out to me that this may be problematic for getting JSON for installed packages as there seems to be no way to actually pass in more than one package id with a single call to the API. Sorry about that.
Instead of --json
or --jsonoutput
, I would suggest implementing --output
that can accept json
as a value and maybe some other values in the future. Just how Azure CLI does today.
If you need to process data returned from the winget cli output, you can use Microsoft.WinGet.Client
powershell package
https://github.com/microsoft/winget-cli/blob/master/src/PowerShell/Microsoft.WinGet.Client/README.md
example usage:
# Install module for winget
Install-Package Microsoft.WinGet.Client
# Show All installed packages
Get-WinGetPackage | Out-GridView
In addition to the PowerShell module, COM APIs are also available for object-based integration.