Skip to content

Instantly share code, notes, and snippets.

@Luiz-Monad
Last active February 26, 2021 20:12
Show Gist options
  • Save Luiz-Monad/a05f7cc82bdd77db3d6a2964eb0c30df to your computer and use it in GitHub Desktop.
Save Luiz-Monad/a05f7cc82bdd77db3d6a2964eb0c30df to your computer and use it in GitHub Desktop.
sort json object by keys
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