Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Out-GridView from JSON file

I want to load a JSON file and show it in a powershell GridView. I was hoping this would work:

'[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' | ConvertFrom-Json | Out-GridView

But that just shows me this unhelpful view: ogv output How can I transform the list into something the grid view understands?

like image 481
jtb Avatar asked Mar 12 '23 06:03

jtb


1 Answers

('[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' | ConvertFrom-Json) | Out-GridView

# or

$converted = '[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' | ConvertFrom-Json 
$converted | Out-GridView

This is a peculiarity of ConvertFrom-Json and anything that uses it implicitly (like Invoke-RestMethod). It doesn't seem to pass objects along the pipeline as you would expect, so you must complete the pipeline to get the objects, and then use them afterwards.

One way to do that is to assign it to a variable, another way is to wrap it in parentheses ( ).

I am not certain why this is the case, but I imagine it's an implementation detail about what it does internally and how it returns its objects.


I was trying to see if I could dig more into this, using ForEach-Object to see what's going wrong, but instead it actually just worked, so here's another way to get it working, but in a single pipeline (by using a superfluous ForEach-Object):

'[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' | ConvertFrom-Json | ForEach-Object { $_ } | Out-GridView

# non-scrolling
'[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' | ConvertFrom-Json | % { $_ } | ogv
like image 192
briantist Avatar answered Mar 19 '23 03:03

briantist