Skip to content

Instantly share code, notes, and snippets.

@BenNeise
Last active August 29, 2015 14:07
Show Gist options
  • Save BenNeise/1a796061044b66bda081 to your computer and use it in GitHub Desktop.
Save BenNeise/1a796061044b66bda081 to your computer and use it in GitHub Desktop.
Takes a (potentially un-formatted) JSON file, sorts on a specified property, then outputs to a sorted list.
function Sort-JSON {
param (
[Parameter(
Mandatory = $true,
Position = 1
)]
[ValidateScript({
Test-Path $_
})]
[string]
$InputJsonFile,
[Parameter(
Mandatory = $true,
Position = 2
)]
[string]
$SortProperty,
[Parameter(
Mandatory = $true,
Position = 3
)]
[string]
$OutputJsonFile
)
begin {
}
process {
try {
(Get-Content -Path $InputJsonFile | ConvertFrom-Json) | Sort-Object -Property $SortProperty | ConvertTo-JSON | Out-File -FilePath $OutputJsonFile -Encoding "ASCII" -Force
}
catch {
Write-Error "Something went wrong"
}
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment