question

WillPittenger avatar image
0 Votes"
WillPittenger asked WillPittenger commented

Recursive sort of ConvertFrom-JSON output

I'm looking to recursively sort the output of ConvertFrom-JSON. So I wrote the code below.

PowerShell
set-alias new New-Object

function Sort-JSONInternal()
{
    param
    (
        [switch]
            $bDontRecurse,

        [parameter(Mandatory, Position=0, ValueFromPipeline)]
        [ValidateNotNull()]
        [object]
            $json
    )

    $propertiesForCurJson = $json | Get-Member -type NoteProperty;

    if($propertiesForCurJson.Count -eq 0)
    {
        write-output $json;
    }
    else
    {
        $ordered = [ordered]@{}

        foreach($curEntry in $propertiesForCurJson | sort "Name")
        {
            $strNameOfCurProp = $curEntry.Name;

            $curVal = $json.$($strNameOfCurProp);

            if($curVal -eq $null)
            {
                $ordered[$strNameOfCurProp] = $curVal;
            }
            elseif($bDontRecurse)
            {
                $ordered[$strNameOfCurProp] = $json.$($curEntry.Name);
            }
            else
            {
                $ordered[$strNameOfCurProp] = Sort-JSONInternal $curVal;
            }
        }

        $jsonSorted = new "PSCustomObject";

        Add-Member -InputObject $jsonSorted -NotePropertyMembers $ordered;

        write-output $jsonSorted;
    }
}

function Sort-JSON()
{
    param
    (
        [switch]
            $bDontRecurse,

        [parameter(Mandatory, Position=0, ValueFromPipeline, ValueFromRemainingArguments)]
        [ValidateNotNullOrEmpty()]
        [object[]]
            $listOfJsonObj
    )

    $listOfJsonObj | foreach {write-output ($_ | Sort-JSONInternal -bDontRecurse:$bDontRecurse)};
}


But the function doesn't seem to work correctly. You call Sort-JSON with one or more JSON objects. Sort-JSONInternal does the actual sorting. If I have code below:

JSON
{
  "t";  [ {"s": 3}, {"s": 5}]
}

It returns something more like:

JSON
{
  "t": {"s": [3, 5]}
}


That's wrong. It shouldn't be summarizing the properties.
Can you tell me what's wrong?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered WillPittenger commented

Hi,

As the value of the "t" property is an array of objects with the "s" property, the value of the "s" property that output in Line 21 is the array (3,5). You could change Line 43 to this

 $ordered[$strNameOfCurProp] = $curVal | ForEach-Object{ Sort-JSONInternal $_ }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ah. Thank you.

0 Votes 0 ·