Format [pscustomobject] instances returned by Invoke-RestMethod or ConvertFrom-Json

后端 未结 2 857

I am trying to create a table from a JSON file I am receiving from a RESTful API.

When I print the property of the json object I get an output like this:

P         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-05 20:58

    You need to add the parent keyname as a property Name to the nested objects:

    $json.Object | ForEach-Object {
      foreach ($p in $_.PSObject.Properties) {
        $p.Value | Select-Object @{n='Name';e={$p.Name}},*
      }
    }
    

    Note that PowerShell will render the output in list form by default, since your objects have more than 4 properties. Pipe it through Format-List -AutoSize to get tabular output.

提交回复
热议问题