我的输出中的owners键(参见OutputFile)应该是一个行分隔的数组,但它是作为一个单行空间分隔的对象/字符串输出的

Script:

function Add-ApplicationOwner
{
    param (
        [string] $App,
        [object] $OutputObject
    )

    # add values to our json output
    $owners = (Get-AzureAdApplicationOwner -ObjectId $App).UserPrincipalName
    $OutputObject | Add-Member -MemberType NoteProperty -Name owners -Value $owners
}

$inputFile = Get-Content -Path "AppInput.json" -Raw | ConvertFrom-Json
$outputFile = New-Object -TypeName PsObject

foreach ($object in $inputFile.PSObject.Properties)
{
    $outputAppList = New-Object -TypeName PsObject

    foreach ($app in $inputFile.New.PsObject.Properties)
    {           

        # create app
        $appRegistration = New-AzureADApplication -DisplayName "TestSPN1"

        #add application info into json object
        $outputAppValues = [PsCustomObject]@{
            app_id = $appRegistration.AppId
        }

        #add application owners by object id
        Add-ApplicationOwner -App $appRegistrationObjectId -OutputObject $outputAppValues

        $outputAppList | Add-Member -MemberType NoteProperty -Name "TestSPN1" -Value $outputAppValues
             
    }

    # add all created apps into json output file
    $outputFile | Add-Member -MemberType NoteProperty -Name "New Applications" -Value $outputAppList

}

$outputFile | ConvertTo-Json | Out-File "AzADAppRegistrationInfo.json" -Append

OutputFile:

{
    "New Applications":  {
                             "TestSPN1":  {
                                              "app_id":  "dsfadfdafa-3afadfdafadsfasd-343",
                                              "owners":  "user1 user2 user3"
                                          }
                         }
}

Desired Output:

{
    "New Applications":  {
                             "TestSPN1":  {
                                              "app_id":  "dsfadfdafa-3afadfdafadsfasd-343",
                                              "owners":  [
                                                   "user1",
                                                   "user2",
                                                   "user3"
                                               ]
                                          }
                         }
}

$owners Variable Examined:

$owners
user1
user2
user3

$owners.gettype()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                                                  
True     True     Object[]                                 System.Array     

When I look at $outputFile.'New Applications' it's as expected

$outputFile.'New Applications' | convertto-json
{
    "TestSPN1":  {
                     "app_id":  "asdfdsfad",
                     "owners":  [
                                    "user1",
                                    "user2",
                                    "user3"
                                ]
                 }
}

When I look at $outputFile it's flattened

$outputFile | convertto-json
{
    "New Applications":  {
                             "TestSPN1":  {
                                              "app_id":  "cc6dgfsdgdsgfsdgfdsa5562614",
                                              "owners":  "user1 user2 user3"
                                          }
                         }
}

推荐答案

对于您的问题,最令人喜欢的解释是-Depth使用默认值.例如,我在$json变量中存储了$outputFile.'New Applications'.

[pscustomobject]@{
    'New Applications' = $json
} | ConvertTo-Json

Results in:

WARNING: Resulting JSON is truncated as serialization has exceeded the set depth of 2.
{
  "New Applications": {
    "TestSPN1": {
      "app_id": "asdfdsfad",
      "owners": "user1 user2 user3"
    }
  }
}

值得指出的是,警告消息仅在较新版本的PowerShell上显示(准确地说是PS 7.1+,感谢mklement0指出).Windows PowerShell默认会在没有任何警告的情况下截断JSON.


但是,如果我们添加1个深度级别(默认值-Depth2):

[pscustomobject]@{
    'New Applications' = $json
} | ConvertTo-Json -Depth 3

Results in:

{
  "New Applications": {
    "TestSPN1": {
      "app_id": "asdfdsfad",
      "owners": [
        "user1",
        "user2",
        "user3"
      ]
    }
  }
}

Json相关问答推荐

Jolt变换将字段移动到数组的每个元素中

无法使用Jolt变换在嵌套的JSON中提取值

无法访问id的第三级json

解析Ansible AWS ec2_security_group中的json以提取安全组ID

如何获取 JSON 对象字段值和同一 JSON 对象的下一个数组中的字段值?

展平多个数组以保持顺序

APIM 生成 JsonArray 到 EventHub

使用 jq 工具将文本从 txt 文件转换为 json

如何使用nifi从json文件中过滤属性

如何从字符串中创建一个逗号分隔的列表,由 API 中的 JSON 对象内的编号空格分隔?

如何在 Flutter 中遍历嵌套的动态 JSON 文件

Vue 3如何将参数作为json发送到axios get

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

如何使用 ConfigurationBuilder 解析现有的 json 字符串(不是文件)

单元测试球衣 Restful Services

Angularjs访问本地json文件

JSON 使用 simplejson 序列化 Django 模型

为不同类型的项目数组正确的 JSON Schema

如何在 Django JSONField 数据上聚合(最小/最大等)?

从 Json 对象 Android 中获取字符串值