Last active
February 26, 2021 20:12
-
-
Save Luiz-Monad/a05f7cc82bdd77db3d6a2964eb0c30df to your computer and use it in GitHub Desktop.
sort json object by keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Sort-Json($json) { | |
$t = ` | |
Get-Content $json -Encoding UTF8 | ` | |
ConvertFrom-Json; | |
$tmp = New-Object -Type psobject; | |
$t.psobject.properties | ` | |
Select-Object -Expand name | ` | |
Sort-Object | ` | |
ForEach-Object { $tmp | ` | |
Add-Member -Type noteproperty -Name $_ -Value $t.$_ | |
}; | |
$tmp | ` | |
ConvertTo-Json | ` | |
Out-File $json -Encoding UTF8NoBOM; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment