6

I have the following json file and I want it sorted by the keys/names. But so far I have been unable to figure out how to actually sort the json object by it's key/name.

Origional Settings.json

{
  "files.trimTrailingWhitespace": true,
  "workbench.startupEditor": "newUntitledFile",
  "editor.tabSize": 4,
  "editor.formatOnSave": true,
  "editor.detectIndentation": false,
  "editor.trimAutoWhitespace": true
}

Code:

# Get Json File
$JsonFile = 'C:\Settings.json'
# Convert from Json File to Json Object
$Json = Get-Content $JsonFile | Out-String | ConvertFrom-Json 
# Sort Json Object (Does Not Work!!!)
$Json = $Json | Sort-Object -Property Name
#Convert Json Object to Json File
$Json | ConvertTo-Json -depth 100 | Set-Content $JsonFile

New Settings.Json

{
  "editor.detectIndentation": false,
  "editor.formatOnSave": true,
  "editor.tabSize": 4,
  "editor.trimAutoWhitespace": true
  "files.trimTrailingWhitespace": true,
  "workbench.startupEditor": "newUntitledFile"
}
3
  • Hi, $Json | Sort-Object -Properties Name displays the sorted lines but does not store them sorted. You need to add $Json = at the beginning for that. Commented Mar 9, 2018 at 19:36
  • 1
    Or just add the Sort to the last line that outputs it. $Json | Sort -Properties Name | ConvertTo-Json -Depth 100 | Set-Content $JsonFile Commented Mar 9, 2018 at 19:38
  • The issue seems to be that the sort-object -property name does not actually sort the object. Commented Mar 9, 2018 at 19:49

3 Answers 3

3

Answer was here: Powershell sort PSObject alphabetically

This issue was that the json file did not have a collection to sort but was a single object whose properties I wanted to sort. Below is the code that works.

  # Build an ordered hashtable of the property-value pairs.
  $SortedByProperties = [ordered] @{}
  Get-Member -Type  NoteProperty -InputObject $Json | Sort-Object Name |
    ForEach-Object { $SortedByProperties[$_.Name] = $Json.$($_.Name) }

  # Create a new object that receives the sorted properties.
  $JsonFileSorted = New-Object PSCustomObject
  Add-Member -InputObject $JsonFileSorted -NotePropertyMembers $SortedByProperties

  $JsonFileSorted | ConvertTo-Json -depth 100 | Set-Content $JsonFile
Sign up to request clarification or add additional context in comments.

Comments

3
$json | Select-Object ($json | Get-Member -MemberType NoteProperty).Name | ConvertTo-Json

2 Comments

It'd be helpful to give some kind of explanation of what this is doing so it's not just a code dump. This appears to be relying on Get-Member to perform the sorting, which is neither obvious nor documented behavior.
Thumbs up because the code posted worked (for me) and was elegant, but I agree with the comment, an explanation would be nice.
1

Just to sum up for quick copy-past option

  $JsonFile = 'C:\data.json'
  $JSON = Get-Content $JsonFile | ConvertFrom-Json

  # Build an ordered hashtable of the property-value pairs. 
  $SortedByProperties = [ordered] @{}
  Get-Member -Type  NoteProperty -InputObject $Json | Sort-Object Name |
    ForEach-Object { $SortedByProperties[$_.Name] = $Json.$($_.Name) }

  # Create a new object that receives the sorted properties. 
  $JsonFileSorted = New-Object PSCustomObject
  Add-Member -InputObject $JsonFileSorted -NotePropertyMembers $SortedByProperties

  $JsonFileSorted | ConvertTo-Json -depth 100 | Set-Content $JsonFile

Thanks @Keith for question and answer

Comments

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.