简体   繁体   中英

How to output the content of a nested JSON hash table to a PSO using PowerShell?

How can I output the entire contents of a nested JSON hash table to a PSO in one line?

$json = @"
{
    "outer": "value1",
    "outerArray": [
        "value2",
        "value3"
    ],
    "outerHash": {
        "inner": "value4",
        "innerArray": [
            "value5",
            "value6"
        ],
        "innerHash": {
            "innermost1": "value7",
            "innermost2": "value8",
            "innermost3": "value9"
        }
    }
}
"@

Current behavior: Only one “level” is displayed

$json | ConvertFrom-Json
outer  outerArray       outerHash                                              
-----  ----------       ---------                                              
value1 {value2, value3} @{inner=value4; innerArray=System.Object[]; innerHash=}

Desired behavior: Recursively expand and display all hash/array content

$json | ConvertFrom-Json
outer  outerArray       outerHash                                              
-----  ----------       ---------                                              
value1 {value2, value3} @{inner=value4; innerArray=@(value5, value6); innerHash=@{innermost1=value7; innermost2=value8; innermost3=value9}}

The following seemed to brush on the subject but did not achieve the desired effect: PowerShell Hashtable from JSON PSCustomObject to Hashtable How to output multiple hash tables in Powershell

User atscripter sends the following message to the owners of the package'ConvertTo-Expression' :

" I came across your " flatten-object " article as I was trying to modify a default behavior of PowerShell. I sought help on stackoverflow and technet and instead of writing code from scratch I was wondering how difficult would it be to tweak "flatten-object" to achieve the desired effect? It seems the function does the difficult part of iterating through the objects I'm just not skilled enough to able to get them to output in the desired format. Your input is greatly appreciated! "

You don't have to rewrite the flatten-object cmdlet orConvertTo-Expression cmdlet for this.
You just need to iterate through the first level and then invoke the ConvertTo-Expression cmdlet (or the native ConvertTo-Json cmdlet) on each property:

In PowerShell Format:

$Properties = @{}
($Json | ConvertFrom-Json).PSObject.Properties |
    ForEach-Object {$Properties.($_.Name) = $_.Value |
        ConvertTo-Expression -Expand -1}
[PSCustomObject]$Properties

Results in:

outer    outerArray        outerHash
-----    ----------        ---------
'value1' 'value2','value3' [PSCustomObject]@{'inner'='value4';'innerArray'='value5','value6';'innerHash'=[PSCustomObject]@{'innermost1'='value7';'innermost2'='value8';'innermost3'='value9'}}

In Json Format:

($Json | ConvertFrom-Json).PSObject.Properties |
    ForEach-Object {$Properties.($_.Name) = $_.Value |
        ConvertTo-Json -Depth 5 -Compress}

Results in a slightly different (Json) format:

outer    outerArray          outerHash
-----    ----------          ---------
"value1" ["value2","value3"] {"inner":"value4","innerArray":["value5","value6"],"innerHash":{"innermost1":"value7","innermost2":"value8","innermost3":"value9"}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM