I am attempting to sort a .json. The json should be sorted first by "Name" and then a sort of everything in "Products". The sort would need to be done via PowerShell.
I have attempted using a derivative of powershell sort json by value with Sort-Object but with no success.
$json = Get-Content '.\.vscode\driverDetails.json' | ConvertFrom-Json
$json.Name = $json.Name | Sort-Object Name
$json.Products = $json.Products | Sort-Object Products
$json | ConvertTo-Json
The json itself should be sorted by "Name" Everything under "Products" is sorted.
Example below
Original Json:
[
{
"Name": "HPE Smart Array P824i-p MR 64-bit controller driver for Microsoft Windows 2016 edition.",
"cpFile": "cp034411",
"version": "6.714.18.0",
"OS": "Microsoft Windows Server 2016",
"Products": [
"HPE ProLiant DL380 Gen10 Server",
"HPE ProLiant DL560 Gen10 Server",
"HPE ProLiant DL385 Gen10 Server\n",
"HPE ProLiant ML350 Gen10 Server",
"HPE ProLiant DL360 Gen10 Server",
"HPE ProLiant DL580 Gen10 Server"
],
"Description": "This driver, MegaSAS2, provides Windows 64-bit support for HPE Smart Array P824i-p MR 64-bit controller",
"Category": "Driver - Storage Controller"
},
{
"Name": "HPE H2xx SAS/SATA Host Bus Adapter Driver for 64-bit Microsoft Windows Server 2016 Editions",
"cpFile": "cp037731",
"version": "2.68.64.2",
"OS": "Microsoft Windows Server 2016",
"Products": [
"HPE ProLiant DL360 Gen9 Server",
"HPE ProLiant DL380 Gen9 Server",
"HPE ProLiant DL560 Gen9 Server",
"HPE ProLiant ML350 Gen9 Server"
],
"Description": "This driver, LSI_SAS2, provides Windows Server 2016 support for HPE H2xx SAS/SATA Host Bus Adapter. ",
"Category": "Driver - Storage Controller"
}
]
Sorted Json:
[
{
"Name": "HPE H2xx SAS/SATA Host Bus Adapter Driver for 64-bit Microsoft Windows Server 2016 Editions",
"cpFile": "cp037731",
"version": "2.68.64.2",
"OS": "Microsoft Windows Server 2016",
"Products": [
"HPE ProLiant DL360 Gen9 Server",
"HPE ProLiant DL380 Gen9 Server",
"HPE ProLiant DL560 Gen9 Server",
"HPE ProLiant ML350 Gen9 Server"
],
"Description": "This driver, LSI_SAS2, provides Windows Server 2016 support for HPE H2xx SAS/SATA Host Bus Adapter. ",
"Category": "Driver - Storage Controller"
},
{
"Name": "HPE Smart Array P824i-p MR 64-bit controller driver for Microsoft Windows 2016 edition.",
"cpFile": "cp034411",
"version": "6.714.18.0",
"OS": "Microsoft Windows Server 2016",
"Products": [
"HPE ProLiant DL360 Gen10 Server",
"HPE ProLiant DL380 Gen10 Server",
"HPE ProLiant DL385 Gen10 Server",
"HPE ProLiant DL560 Gen10 Server",
"HPE ProLiant DL580 Gen10 Server",
"HPE ProLiant ML350 Gen10 Server"
],
"Description": "This driver, MegaSAS2, provides Windows 64-bit support for HPE Smart Array P824i-p MR 64-bit controller",
"Category": "Driver - Storage Controller"
}
]
CodePudding user response:
To The json itself should be sorted by "Name":
$Json = $Json |Sort-Object Name
To sort of everything in "Products":
$Json.Foreach{ $_.Products = $_.Products |Sort-Object }