对json的多个部分进行排序

bmp9r5qi  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(118)

我正在尝试对一个. json进行排序。json应该首先按“名称”排序,然后按“产品”中的所有内容排序。排序需要通过PowerShell完成。
我曾尝试使用powershell sort json by value with Sort-Object的派生,但没有成功。

$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

json本身应该按“名称”排序,“产品”下的所有内容都是排序的。
示例如下
原始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"
  }
]

排序的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"
    }
]
vs91vp4v

vs91vp4v1#

To * json本身应该按“名称”排序 *:

$Json = $Json |Sort-Object Name

要 * 对“产品”中的所有内容进行排序 *:

$Json.Foreach{ $_.Products = $_.Products |Sort-Object }

相关问题