在JSON输出中将单个结果强制为数组

时间:2019-04-02 08:29:24

标签: arrays json powershell

我正在从SQL数据库中检索库存数据,我希望将其转换为JSON以进行进一步处理。

对于操作系统,处理器和应用程序有单独的查询(添加和删除信息)。

对于OS,通常返回一行;对于处理器,通常返回一行或多行;对于应用程序,通常返回几行。

数据的检索是这样完成的(这部分工作正常):

$deviceOperatingSystem = Invoke-Sqlcmd -ServerInstance $sqlServer -Database $sqlDatabase -Query $inventoryOperatingSystemQuery -credential $databaseCredentials -Variable $variables
$deviceProcessors = Invoke-Sqlcmd -ServerInstance $sqlServer -Database $sqlDatabase -Query $inventoryProcessorsQuery -credential $databaseCredentials -Variable $variables
$deviceApplications = Invoke-Sqlcmd -ServerInstance $sqlServer -Database $sqlDatabase -Query $inventoryApplicationsQuery -credential $databaseCredentials -Variable $variables

然后将其馈送到这样的数组:

$deviceInventoryOutput = @()
    $deviceInventoryOutput += [PSCustomObject]@{
        'OperatingSystem'    = $deviceOperatingSystem
        'Processors'         = $deviceProcessors     
        'Applications'       = $deviceApplications   
                    }

,输出如下所示:

{
    "OperatingSystem":  {
                            "Caption":  "Microsoft Windows 10 Enterprise",
                            "Version":  "10.0.16299",
                            "BuildNumber":  "16299",
                            "ServicePackMajorVersion":  0,
                            "Language":  1031
                        },
    "Processors":  {
                       "Name":  "Intel(R) Core(TM) i3-6100U CPU @ 2.30GHz",
                       "Description":  "Intel64 Family 6 Model 78 Stepping 3",
                       "Manufacturer":  "GenuineIntel",
                       "NumberOfCores":  1,
                       "NumberOfLogicalCores":  1
                   },
    "Applications":  [
                         {
                             "Name":  "IM.order",
                             "Publisher":  "Ingram Micro Distribution GmbH",
                             "Version":  "6.41.761.0"
                         },
                         {
                             "Name":  "JTL-Wawi",
                             "Publisher":  "JTL-Software-GmbH",
                             "Version":  "1.2.3.7"
                         },...
}

即使只有一个条目,接收方API也会将处理器的信息视为一个数组,因此我的示例实际上应如下所示:

{
    "Processors":  [{
                       "Name":  "Intel(R) Core(TM) i3-6100U CPU @ 2.30GHz",
                       "Description":  "Intel64 Family 6 Model 78 Stepping 3",
                       "Manufacturer":  "GenuineIntel",
                       "NumberOfCores":  1,
                       "NumberOfLogicalCores":  1
                   }]
}

我试图像这样包装处理器变量

'Processors'         = @($deviceProcessors)

但这没有太大帮助。我还尝试将$ deviceProcessors初始化为一个数组,例如:

$deviceProcessors = @()
$deviceProcessors += Invoke-Sqlcmd -ServerInstance $sqlServer -Database $sqlDatabase -Query $inventoryProcessorsQuery -credential $databaseCredentials -Variable $variables

但是,没有,没有解决。

 $deviceProcessors.GetType() returns System.Data.DataRow
 $deviceApplications.GetType() returns System.Object[]

如果查询仅返回一个结果,则预期输出应在处理器信息的周围带有[]。

1 个答案:

答案 0 :(得分:0)

以下代码段即可完成工作:

$deviceInventoryOutput = @()
$deviceInventoryOutput += [PSCustomObject]@{
    'OperatingSystem'    = $deviceOperatingSystem
    'Processors'         = $deviceProcessors     
    'Applications'       = $deviceApplications   
}
# possible additional `$deviceInventoryOutput += [PSCustomObject]@{…}` here

for ( $i = 0; $i -lt $deviceInventoryOutput.Count; $i++ ) {
  foreach ( $aux in $( $deviceInventoryOutput[$i] | 
                            Get-Member -MemberType NoteProperty ).Name ) {
      if ( $deviceInventoryOutput[$i].$aux.Gettype().name -ne 'Object[]' ) {
          $deviceInventoryOutput[$i].$aux = @($deviceInventoryOutput[$i].$aux)
      }
  }
}
$deviceInventoryOutput | ConvertTo-Json -Depth 2