JSON Adapter Feedback Provider

Steven Bucher

Jim Truher

JSON Adapter Feedback Provider Release

Caution


This blog post is outdated, please see the updated blog, PowerShell Adapter Feedback Provider Blog

 

We are excited to announce the first release of our JSON Adapter Feedback Provider! If you are unfamiliar with what feedback providers are, check out this blog describing them, here.

Note


Feedback Providers are an experimental feature of 7.4-preview2+ and so you will be required to use one of the 7.4 previews for JSON Adapters to work.

Installing JSON Adapter Feedback Provider

First to get the latest PowerShell preview for this to work on you can download them on our GitHub release page here.

The release is available from the PowerShell Gallery.

Use the following command to install JsonAdapter using PowerShellGet v2.x:

Install-Module -Name Microsoft.PowerShell.JsonAdapter -AllowPrerelease 

If you are using PSResourceGet, you can use the following command:

Install-PSResource -Name Microsoft.PowerShell.JsonAdapter -PreRelease

To use it you will need to import the module into your session via:

Import-Module Microsoft.PowerShell.JsonAdapter

We encourage you to include the import message in your $PROFILE so it can persistently be loaded in every PowerShell session you start. If you have Visual Studio Code installed, type code $PROFILE to edit your profile or use your choice of editor.

What are JSON Adapters?

A JSON adapter is a script that can parse the text output of a native executable and convert it to PowerShell Object. JSON adapters can be made for any command, it is just required to use the exact name of the command as the prefix to the script. The script will have to be named like so <name of command>-json.ps1 to be identified by the JSON adapter utility. This script’s file location must also be added to your $env:PATH variable to be found.

Creating a JSON Adapter

For example, say you are on a Mac and want to use the command vm_stat like a PowerShell object. If you add the following to a file called vm_stat-json.ps1 and add the location of this file to your $env:PATH variable, the JSON Adapter feedback provider will identify it as a possible suggestion for vm_stat.

[CmdletBinding()]
param ( [Parameter(ValueFromPipeline=$true)][string]$inputObject )
BEGIN {
    $h = @{}
}

PROCESS {
    if ( $inputObject -match "^Mach Virtual") {
        if ($inputObject -match "page size of (\d+) ") {
            $h['PageSize'] = [int]$matches[1]
        }
    }
    else {
        $k,$v = $inputObject -split ":"
        $AdjustedK = ($k -replace "[ -]","_").trim() -replace '"'
        $AdjustedV = "$v".Trim() -replace "\.$"
        $h[$AdjustedK] = [int64]$AdjustedV
    }
}

END {
    [pscustomobject]$h
}

 

This is what the experience looks like in the shell.

VM stat screenshot

JC

JC or JSON Converter, is a command line utility that can convert text to JSON for variety of command line tools. You can find instructions on how to install jc and a full list of supported commands on the repo of jc. It can be a great tool to use to convert the outputs without writing a JSON Adapter yourself. The JSON Adapter module supports using jc as a JSON Adapter if the user has it installed. This means if you have the jc utility installed and use a command that is supported by JC, the JSON Adapter feedback provider will suggest using JC piped to ConvertFrom-JSON.

It is important to note that not all jc supported utilities are supported. The list of supported commands is:

"arp", "cksum", "crontab", "date", "df", "dig", "dir", "du", "file", "finger",
"free", "hash", "id", "ifconfig", "iostat", "jobs", "lsof", "mount", "mpstat",
"netstat", "route", "stat", "sysctl", "traceroute", "uname", "uptime", "w", "wc",
"who", "zipinfo"

Additionally, you will need to use the appropriate parameters that jc requires to work properly. For example, if you want to use jc with uname, you will need to use uname -a as that is what jc requires to properly convert the output to JSON.

Predictive IntelliSense Support

We have also added predictive IntelliSense support for the JSON Adapter feedback provider. This means after a JSON Adapter feedback provider is triggered, as you type the command name again, Predictive Intellisense will suggest the feedback command to you. This is a great way to easily try the suggestion after a JSON Adapter feedback provider is triggered.

screenshot showing predictive intellisense support

Feedback

As this is our very first release, we know there may be issues that arise. We definitely look forward to your feedback and suggestions! You can provide feedback on the repo for this project here. Many things are subject to change as we are in early development of this. Give it a try!

Jim Truher and Steven Bucher

PowerShell Team

5 comments

Discussion is closed. Login to edit/delete existing comments.

  • Kris Borowinski 0

    This is great! Will be helpful to build Crescendo modules!

  • Ilya Sher 0

    While I was thinking about implementing something similar in Next Generation Shell, PowerShell just did it.

    I did learn that there is a shortcut though: you can hardcode the list of supported commands. I was thinking about querying jc at build time to get the list of supported commands but this is way simpler.

    jc solves the structured data problem, converting from text to structured data. I would like to encourage everyone to think about, and maybe implement, the next phase/layer: semantically meaningful objects. Without semantics, one can not implement an interactive shell. By interactive I don’t mean what everybody accepts now as an interactive shell, which still utilizes the telegraph style interaction paradigm. By interactive I mean that the user can interact (click, enter, right click) with objects on the screen. To provide meaningful interaction, the shell needs to know (or allow plugins to do that) the semantic meaning of the objects. That is how it would be able to provide the appropriate menus and actions for the objects on the screen. More thoughts about shell UI are at NGS wiki.

  • Bartek Bielawski 0

    Looking purely from PowerShell perspective, code proposed makes no sense. Either we don’t need begin/process/end, or only last object picked from the pipeline is going to be returned.
    Also it would be nice to mention that this feature works only in 7.4 (at least when I tried in 7.3, I couldn’t import the module).

    • Bartek Bielawski 0

      OK, did some testing and as expected – returning object (and most likely creating it) should all happen in process block.
      Two things that may help:
      – I wrote article about using $Matches directly in situations like this 10y ago (I’m getting old…) : https://becomelotr.wordpress.com/2013/01/07/quick-tip-matches-is-enough/
      – it would be great if on windows tool would pick-up suggestion for both command and command.exe, else we would need to have json for both variations, feels like it should be that hard to support both authors that do foo.exe-json.ps1 and foo-json.ps1

Feedback usabilla icon