将PowerShell数组合并到PSComObject(JSON)

amrnrhlw  于 2023-06-25  发布在  Shell
关注(0)|答案(2)|浏览(141)

我正在努力构建一个函数来构造API调用的json有效负载。json payload将有一个主“成员”,但我需要添加一个子成员,它实际上是一个json数组。我有这个 * 几乎 * 工作,但我一直无法让它 * 完全 * 工作。
以下是两个示例(产生不同但不正确的结果):
此示例生成了正确的结构(除了数组周围需要的开头和结尾{}:

[PSCustomObject] $contactMain = @{}
[PSCustomObject] $contactDetail_Native = @{}
[array] $contactDetail_Custom = @()

$contactDetail_Custom = `
    [PSCustomObject]@{
        field = '1'
        value = '1001'
    }

$contactDetail_Custom += $contactDetail_Custom

$contactDetail_Custom = `
    [PSCustomObject]@{
        field = '2'
        value = '1002'
    }

$contactDetail_Custom += $contactDetail_Custom

$contactDetail_Native = `
    [ordered]@{
        email     = 'brucebanner@myfakeemail.com'
        lastName  = 'Banner'
        firstName = 'Bruce'
    }

$contactDetail_Native.Add('fieldValues', $contactDetail_Custom)

$contactMain | Add-Member -MemberType NoteProperty -Name 'contact' -Value $contactDetail_Native -Force
#$contactMain | Add-Member -MemberType NoteProperty -Name 'fieldValues' -Value $contactDetail_Custom -Force

$contactMain = $contactMain | ConvertTo-Json
$contactMain

本例呈现值,但fieldValues需要联系人成员内:

[PSCustomObject] $contactMain = @{}
[PSCustomObject] $contactDetail_Native = @{}
[array] $contactDetail_Custom = @()

$contactDetail_Custom = `
    [PSCustomObject]@{
        field = '1'
        value = '1001'
    }

$contactDetail_Custom += $contactDetail_Custom

$contactDetail_Custom = `
    [PSCustomObject]@{
        field = '2'
        value = '1002'
    }

$contactDetail_Custom += $contactDetail_Custom

$contactDetail_Native = `
    [ordered]@{
        email     = 'brucebanner@myfakeemail.com'
        lastName  = 'Banner'
        firstName = 'Bruce'
    }

#$contactDetail_Native.Add('fieldValues', $contactDetail_Custom)

$contactMain | Add-Member -MemberType NoteProperty -Name 'contact' -Value $contactDetail_Native -Force
$contactMain | Add-Member -MemberType NoteProperty -Name 'fieldValues' -Value $contactDetail_Custom -Force

$contactMain = $contactMain | ConvertTo-Json
$contactMain

需要渲染的方式是这样的:

{"contact": {
    "firstName":  "Bruce",
    "lastName":  "Banner",
    "email":  "brucebanner@myfakeemail.com",
    "fieldValues":  [
                        {
                            "field":  "1",
                            "value":  "1001"
                        },
                        {
                            "field":  "2",
                            "value":  "1002"
                        }
                    ]
}}

我做错了什么?
谢谢!

gkn4icbw

gkn4icbw1#

您可能希望使用此ConvertTo-Expression来构建模板和/或获得PowerShell结构的印象:

Install-Script -Name ConvertTo-Expression
$YourJson |ConvertFrom-Json |ConvertTo-Expression
[pscustomobject]@{contact = [pscustomobject]@{
        firstName = 'Bruce'
        lastName = 'Banner'
        email = 'brucebanner@myfakeemail.com'
        fieldValues =
            [pscustomobject]@{
                field = '1'
                value = '1001'
            },
            [pscustomobject]@{
                field = '2'
                value = '1002'
            }
    }}

要自动构建fieldValues,你可以这样做:

function Get-CustomDetail {
    param([Parameter(ValueFromPipeline=$true)][String]$Field)
    process {
        [pscustomobject]@{
            field = $Field
            value = "100$Field"
        }
    }
}

[pscustomobject]@{contact = [pscustomobject]@{
        firstName = 'Bruce'
        lastName = 'Banner'
        email = 'brucebanner@myfakeemail.com'
        fieldValues = @(1,2 |Get-CustomDetail) # @(...) forces an array for single items
    }
} |ConvertTo-Json -Depth 9
{
  "contact": {
    "firstName": "Bruce",
    "lastName": "Banner",
    "email": "brucebanner@myfakeemail.com",
    "fieldValues": [
      {
        "field": "1",
        "value": "1001"
      },
      {
        "field": "2",
        "value": "1002"
      }
    ]
  }
}
zmeyuzjn

zmeyuzjn2#

看起来我自己就能搞清楚了。解决方案是使用我提供的第一个代码示例在ConvertTo-Jsoncmdlet上设置Depth参数。
下面是生成我需要的结果的最终版本:

[PSCustomObject] $contactMain          = @{}
[PSCustomObject] $contactDetail_Native = @{}
[array]          $contactDetail_Custom = @{}
[array]          $contactDetail        = @()

$contactDetail_Custom = `
    [PSCustomObject]@{
        field = '1'
        value = '1001'
    }

$contactDetail += $contactDetail_Custom

$contactDetail_Custom = `
    [PSCustomObject]@{
        field = '2'
        value = '1002'
    }

$contactDetail += $contactDetail_Custom

$contactDetail_Native = `
    [ordered]@{
        email     = 'brucebanner@myfakeemail.com'
        lastName  = 'Banner'
        firstName = 'Bruce'
    }

$contactDetail_Native.Add('fieldValues', $contactDetail)

$contactMain | Add-Member -MemberType NoteProperty -Name 'contact' -Value $contactDetail_Native -Force

$contactMain = $contactMain | ConvertTo-Json -Depth 100
$contactMain

相关问题