{"id":20040,"date":"2023-07-10T13:00:44","date_gmt":"2023-07-10T21:00:44","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell\/?p=20040"},"modified":"2023-08-28T14:48:57","modified_gmt":"2023-08-28T22:48:57","slug":"json-adapter-feedback-provider","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/json-adapter-feedback-provider\/","title":{"rendered":"JSON Adapter Feedback Provider"},"content":{"rendered":"<h2>JSON Adapter Feedback Provider Release<\/h2>\n<p><div class=\"alert alert-danger\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--ErrorBadge\"><\/i><strong>Caution<\/strong><\/p>This blog post is outdated, please see the updated blog, <a href=\"https:\/\/devblogs.microsoft.com\/powershell\/powershell-adapter-feedback-provider\/\">PowerShell Adapter Feedback Provider Blog<\/a><\/div><\/p>\n<p>&nbsp;<\/p>\n<p>We are excited to announce the first release of our JSON Adapter Feedback Provider! If you are\nunfamiliar with what feedback providers are, check out this blog describing them, <a href=\"https:\/\/devblogs.microsoft.com\/powershell\/what-are-feedback-providers\/\">here<\/a>.<\/p>\n<p><div class=\"alert alert-primary\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Note<\/strong><\/p>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.<\/div><\/p>\n<h2>Installing JSON Adapter Feedback Provider<\/h2>\n<p>First to get the latest PowerShell preview for this to work on you can download them on our GitHub release page <a href=\"https:\/\/github.com\/PowerShell\/PowerShell\/tags\">here<\/a>.<\/p>\n<p>The release is available from the <a href=\"https:\/\/www.powershellgallery.com\/packages\/Microsoft.PowerShell.JsonAdapter\/0.1.0\">PowerShell Gallery<\/a>.<\/p>\n<p>Use the following command to install <strong>JsonAdapter<\/strong> using <strong>PowerShellGet<\/strong> v2.x:<\/p>\n<pre><code class=\"language-powershell\">Install-Module -Name Microsoft.PowerShell.JsonAdapter -AllowPrerelease <\/code><\/pre>\n<p>If you are using <strong>PSResourceGet<\/strong>, you can use the following command:<\/p>\n<pre><code class=\"language-powershell\">Install-PSResource -Name Microsoft.PowerShell.JsonAdapter -PreRelease<\/code><\/pre>\n<p>To use it you will need to import the module into your session via:<\/p>\n<pre><code>Import-Module Microsoft.PowerShell.JsonAdapter<\/code><\/pre>\n<p>We encourage you to include the import message in your <code>$PROFILE<\/code> so it can persistently be loaded\nin every PowerShell session you start. If you have Visual Studio Code installed, type\n<code>code $PROFILE<\/code> to edit your profile or use your choice of editor.<\/p>\n<h2>What are JSON Adapters?<\/h2>\n<p>A JSON adapter is a script that can parse the text output of a native executable and convert it to\nPowerShell Object. JSON adapters can be made for\nany command, it is just required to use the exact name of the command as the prefix to the script.\nThe script will have to be named like so <code>&lt;name of command&gt;-json.ps1<\/code> to be identified by the JSON\nadapter utility. This script&#8217;s file location must also be added to your <code>$env:PATH<\/code> variable to be\nfound.<\/p>\n<h2>Creating a JSON Adapter<\/h2>\n<p>For example, say you are on a Mac and want to use the command <code>vm_stat<\/code> like a PowerShell object. If\nyou add the following to a file called <code>vm_stat-json.ps1<\/code> and add the location of this file to your\n<code>$env:PATH<\/code> variable, the JSON Adapter feedback provider will identify it as a possible suggestion\nfor <code>vm_stat<\/code>.<\/p>\n<pre><code class=\"language-powershell\">[CmdletBinding()]\r\nparam ( [Parameter(ValueFromPipeline=$true)][string]$inputObject )\r\nBEGIN {\r\n    $h = @{}\r\n}\r\n\r\nPROCESS {\r\n    if ( $inputObject -match \"^Mach Virtual\") {\r\n        if ($inputObject -match \"page size of (\\d+) \") {\r\n            $h['PageSize'] = [int]$matches[1]\r\n        }\r\n    }\r\n    else {\r\n        $k,$v = $inputObject -split \":\"\r\n        $AdjustedK = ($k -replace \"[ -]\",\"_\").trim() -replace '\"'\r\n        $AdjustedV = \"$v\".Trim() -replace \"\\.$\"\r\n        $h[$AdjustedK] = [int64]$AdjustedV\r\n    }\r\n}\r\n\r\nEND {\r\n    [pscustomobject]$h\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>This is what the experience looks like in the shell.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2023\/07\/vm_stat.png\" alt=\"VM stat screenshot\" \/><\/p>\n<h2>JC<\/h2>\n<p>JC or JSON Converter, is a command line utility that can convert text to JSON for variety of command\nline tools. You can find instructions on how to install <code>jc<\/code> and a full list of supported commands\non the <a href=\"https:\/\/github.com\/kellyjonbrazil\/jc\">repo of jc<\/a>. It can be a great tool to use to convert the outputs without writing a JSON\nAdapter yourself. The <code>JSON Adapter<\/code> module supports using <code>jc<\/code> as a JSON Adapter if the user has it\ninstalled. This means if you have the <code>jc<\/code> utility installed and use a command that is supported by JC, the JSON\nAdapter feedback provider will suggest using JC piped to <code>ConvertFrom-JSON<\/code>.<\/p>\n<p>It is important to note that not all jc supported utilities are supported. The list of supported commands is:<\/p>\n<pre><code>\"arp\", \"cksum\", \"crontab\", \"date\", \"df\", \"dig\", \"dir\", \"du\", \"file\", \"finger\",\r\n\"free\", \"hash\", \"id\", \"ifconfig\", \"iostat\", \"jobs\", \"lsof\", \"mount\", \"mpstat\",\r\n\"netstat\", \"route\", \"stat\", \"sysctl\", \"traceroute\", \"uname\", \"uptime\", \"w\", \"wc\",\r\n\"who\", \"zipinfo\"<\/code><\/pre>\n<p>Additionally, you will need to use the appropriate parameters that jc requires to work properly. For\nexample, if you want to use <code>jc<\/code> with <code>uname<\/code>, you will need to use <code>uname -a<\/code> as that is what <code>jc<\/code>\nrequires to properly convert the output to JSON.<\/p>\n<h2>Predictive IntelliSense Support<\/h2>\n<p>We have also added predictive IntelliSense support for the JSON Adapter feedback provider. This\nmeans after a JSON Adapter feedback provider is triggered, as you type the command name again,\nPredictive Intellisense will suggest the feedback command to you. This is a great way to easily try\nthe suggestion after a JSON Adapter feedback provider is triggered.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2023\/07\/predictiveintellisense.png\" alt=\"screenshot showing predictive intellisense support\" \/><\/p>\n<h2>Feedback<\/h2>\n<p>As this is our very first release, we know there may be issues that arise. We definitely look\nforward to your feedback and suggestions! You can provide feedback on the repo for this project\n<a href=\"https:\/\/github.com\/PowerShell\/JsonAdapter\">here<\/a>. Many things are subject to change as we are in early development of this. Give it a try!<\/p>\n<p>Jim Truher and Steven Bucher<\/p>\n<p>PowerShell Team<\/p>\n<p><!-- Link references --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog outlines a new feedback provider developed by the PowerShell Team to inform users of native executables that can output `JSON`.<\/p>\n","protected":false},"author":98569,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3189,248],"class_list":["post-20040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-feedback-providers","tag-powershell"],"acf":[],"blog_post_summary":"<p>This blog outlines a new feedback provider developed by the PowerShell Team to inform users of native executables that can output `JSON`.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/20040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/98569"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=20040"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/20040\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=20040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=20040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=20040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}