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

3
  • 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. Commented Feb 4, 2019 at 5: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. Commented Feb 4, 2019 at 9:36
  • @Lee_Dailey @ AnsgarWiechers - Thanks for pitching in! Commented Feb 11, 2019 at 5:49

1 Answer 1

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"}}
Sign up to request clarification or add additional context in comments.

1 Comment

Clean, simple and does exactly what I need. Thank you Ron for taking the time to do this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.