Last active
August 29, 2015 14:07
-
-
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.
This file contains 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 { | |
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