jsonpowershellpsobjectselect-object

Select-Object Data Failure From Succesful Invoke-RestMethod


I have a working call to a rest service using Invoke-RestMethod -Uri https://.... The call's result, in JSON, can be piped the data to Format-Table -Property ... and data is shown.

But when Select-Object -Property ... is used after the invoke with the same parameters, the PSObject has the columns but no data for them. If I use a different webservice the call will work.

enter image description here

What could be causing the PSObject to not show any values?


Working Example on public rest web services

Invoke-RestMethod -Uri https://jsonplaceholder.typicode.com/todos/1 |
Select-Object -Property title

Result

@{title=delectus aut autem}

New Failure Different API

Invoke-RestMethod -Uri https://cat-fact.herokuapp.com/facts | Select-Object -Property text

enter image description here


Solution

  • You've stumbled upon an unholy combination of two PowerShell oddities when converting JSON arrays:

    To demonstrate the problem with a simple example:

    # Windows PowerShell only:
    # Because ConvertFrom-Json sends an *array* (of 2 custom objects) through
    # the pipeline, Select-Object looks for property .text on the *array* -
    # and can't find it.
    # The same applies to Invoke-RestMethod (also still in 
    # PowerShell (Core) as of v7.2)
    PS> ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]' | Select-Object text
    
    text
    ----
           # NO VALUES 
    

    A simple workaround is to enclose the ConvertFrom-Json / Invoke-RestMethod call in (...), which forces enumeration of the array, causing Select-Object to work as expected.:

    # (...) forces enumeration
    PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]') | Select-Object text
    
    text
    ----
    a
    b
    

    Note that a command such as Select-Object -Property text (without -ExpandProperty) still output custom objects that have a .text property, not the .text property values.

    If all you're interested in is property values, the solution is simpler, because you can use the above-mentioned member-access enumeration directly on the array:

    # Using .<propName> on an array (collection) implicitly returns the
    # property values from the *elements* of that collection (member-access enumeration).
    PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]').text
    a
    b
    

    Note how the output now has no text header, because it is mere string values that are being output, not custom objects.