Recursive sort of ConvertFrom-JSON output

Will Pittenger 306 Reputation points
2021-05-18T14:59:00.54+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-05-19T09:43:48.023+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.