powershellconvertfrom-json

Powershell ConvertFrom-Json omitting Depth > 1


Given this JSON

{"name":"//iam.googleapis.com/projects/aw-b9a-8231-4086-a297-ca0/serviceAccounts/1081069135974252","asset_type":"iam.googleapis.com/ServiceAccount","resource":{"version":"v1","discovery_document_uri":"https://iam.googleapis.com/$discovery/rest","discovery_name":"ServiceAccount","parent":"//cloudresourcemanager.googleapis.com/projects/1090951556423","data":{"email":"default@aw-d1eb6b9a-8231-4086-a297-ca0.iam.gserviceaccount.com","name":"projects/aw-d1eb6b9a-8231-4086-a297-ca0/serviceAccounts/default@aw-d1eb6b9a-8231-4086-a297-ca0.iam.gserviceaccount.com","oauth2ClientId":"108102635374252","projectId":"aw-d1eb6b9a-8231-4086-a297-ca0","uniqueId":"108108526913531974252"}},"ancestors":["projects/1090951556423","folders/940897400840","folders/140924730741","organizations/437515948226"],"update_time":"2021-02-08T18:23:18Z"}

I'm trying to use the following snippet from Windows command window,

powershell -nologo -executionpolicy bypass "& {foreach($line in Get-Content temp.txt) {ConvertFrom-Json -InputObject $line}}" > temp2.txt

To get the retrieved keys and values into a file (temp2.txt). However, ConvertFrom-Json only gets the first hierarchic level of the JSON, now down in the data array. I have tried -Depth 3 but get this error:

At line:1 char:81
+ ... ontent temp.txt) {ConvertFrom-Json  -InputObject $line -Depth 4 -Comp ...
+                                                            ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertFrom-Json], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ConvertFromJsonCommand```


Solution

  • As Jeroen Mostert points out in a comment, only in PowerShell (Core) v6.2+ does ConvertFrom-Json have a -Depth parameter. However, even if it were available to you, it would neither be needed nor would it help, because ConvertFrom-Json correctly parses JSON with a nesting depth up to 1024 levels by default.


    Your problem stems from the fact that >, PowerShell's redirection operator is in effect an alias of Out-File, which, when given complex objects as input, saves them using the same for-display representation you would see in the console - and with a deeply nested input object such as yours the resulting interpretation can be unhelpful (since your input object has more than 4 top-level properties, it is implicitly formatted with Format-List).

    So the question for you to decide is:

    If you're primarily looking to pretty-print the input JSON, you can use a ConvertFrom-Json / ConvertTo-Json round-trip operation, whose output will be pretty-printed by default:

    powershell -executionpolicy bypass -c "ConvertFrom-Json (Get-Content -Raw temp.txt) | ConvertTo-Json" > temp2.txt
    

    Note: