2

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

deralbert
  • 856
  • 2
  • 15
  • 33
atscripter
  • 23
  • 5
  • 1
    you may want to add a note that the `innerarray` does show up as an array when you address `.outerhash`. ///// i can't find anything about why it is not expanded when displaying the higher level, tho. – Lee_Dailey Feb 04 '19 at 05:58
  • 1
    PowerShell default output formatting doesn't do that for you. I'd say you need to write a custom formatting function that recurses into hashes and mangles the data-(sub-)structure into a string. – Ansgar Wiechers Feb 04 '19 at 09:36
  • @Lee_Dailey @ AnsgarWiechers - Thanks for pitching in! – atscripter Feb 11 '19 at 05:49

1 Answers1

1

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 or ConvertTo-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"}}
iRon
  • 20,463
  • 10
  • 53
  • 79