A simple module that will reorder a json keys in alphabetical order
Install-Module SortJson -Force
WARNING: Specifying lists of properties that do not exist and the -OnlyListedProperties parameter combined will return an empty json
Sort-Json
parameter | alias |
---|---|
-PropertyStartList | StartList, First |
-PropertyEndList | EndList, Last |
-OnlyListedProperties | OnlyListed |
-SortAlphabetically | Ascending |
-IgnoreCaseSensitivity | CaseInSensitive |
-Recursive | Recurse |
$json = @"
{"displayName":"A test of json","policyType":"BuiltIn","mode":"Indexed","description":"A test to see if the json get ordered","metadata":{"version":"1.1.0","category":"Ordering"},"parameters":{"effect":{"type":"String","metadata":"@{displayName=Ordering; description=Order or not order that is the question}","allowedValues":"Yes No","defaultValue":"Yes"}},"policyRule":{"if":{"allOf":" "},"then":{"effect":"[parameters('order')]"}},"scopetag":["order","json","default","unorder"]}
"@
#Original JSON...
$json | ConvertFrom-Json | ConvertTo-Json -Depth 100
#Sorted JSON using pipeline...
$json | Format-JsonOrder -SortAlphabetically -Recursive
$json = @"
{"displayName":"A test of json","policyType":"BuiltIn","mode":"Indexed","description":"A test to see if the json get ordered","metadata":{"version":"1.1.0","category":"Ordering"},"parameters":{"effect":{"type":"String","metadata":"@{displayName=Ordering; description=Order or not order that is the question}","allowedValues":"Yes No","defaultValue":"Yes"}},"policyRule":{"if":{"allOf":" "},"then":{"effect":"[parameters('order')]"}},"scopetag":["order","json","default","unorder"]}
"@
#Sorted JSON using Alias...
$json | Sort-Json -First @('policyType','displayName') -Last @('policyRule') -Ascending -Recurse