【问题标题】:Change JSON property to an array if not an array powershell如果不是数组 powershell,则将 JSON 属性更改为数组
【发布时间】:2021-01-21 11:53:38
【问题描述】:

我有一个 JSON,我想检查它是否为数组,如果不是,我想更新 JSON 并将其更改为数组

{
    "Customer": [{
        "id": "123"
    }],
 "address": {
    "type": "home",
    "name": "what",
    "description": "",
    "adi:water": {
      "type": "comp",
      "location": "grass"
    },
    "att": [
      {
        "name": "cat",
        "type": "int"
      },
      {
        "name": "ds",
        "type": "string"
      }
    ]
  }
}

#For example address is not an array, I want to change so address is an array of one single object

到目前为止的代码

$File = ((Get-Content -Encoding UTF8 -path Test.json -Raw)

$File = $File| ConvertFrom-Json
$File.address = $File.address | Select-Object * -ExcludeProperty streets #This line changes it to an object instead of an array

我希望地址是一个数组。我可以在 powershell 中这样做吗?

【问题讨论】:

    标签: json powershell powershell-core


    【解决方案1】:

    使用ForEach-Object 并重新分配address 属性值,方法是将其包含在@(...)array-subexpression operator 中,以确保它是一个数组(如果它已经一个数组,没有任何变化[1]):

    @'
    {
      "Customer": [
          {
              "id": "123"
          }
      ],
      "address": {
          "type": "home",
          "name": "what",
          "description": "",
          "adi:water": {
              "type": "comp",
              "location": "grass"
          },
          "att": [
              {
                  "name": "cat",
                  "type": "int"
              },
              {
                  "name": "ds",
                  "type": "string"
              }
          ]
      }
    }
    '@ | ConvertFrom-Json | 
      ForEach-Object { 
        $_.address = @($_.address | Select-Object * -ExcludeProperty streets)
        $_ 
      } |
        ConvertTo-Json -Depth 4
    

    注意赋值后的独立$_ 语句:它确保(修改的)输入对象也是输出(到下一个管道段),使用PowerShell的隐式输出特性 - 见this answer

    注意ConvertTo-Json默认将序列化深度限制为2,因此上面使用-Depth 4来防止数据丢失。一般来说,请记住您可能必须将-Depth 参数传递给ConvertTo-Json 以防止数据丢失 - 请参阅this post[2]

    上面的结果如下,表明address 属性现在是一个数组:

    {
      "Customer": [
        {
          "id": "123"
        }
      ],
      "address": [
        {
          "type": "home",
          "name": "what",
          "description": "",
          "adi:water": {
            "type": "comp",
            "location": "grass"
          },
          "att": [
            {
              "name": "cat",
              "type": "int"
            },
            {
              "name": "ds",
              "type": "string"
            }
          ]
        }
      ]
    }
    

    [1] 从技术上讲,创建了现有数组的(浅)副本,但这在这里没有任何区别。
    要了解有关 @() 运算符的更多信息,请参阅 this answer

    [2] 这个要求很繁琐,很容易漏掉,但是为了向后兼容没有改变。但是,当(可能是默认的)-Depth 值不足时,PowerShell 7.1 版至少会发出一个警告:请参阅this PRGitHub issue #8393 中的相关讨论.
    此外,在 v7.1 之后的某个时间点,将 JSON cmdlet 的实现从 Newtonsoft.Json 库移至新的(ish)内置 System.Text.Json API 时,可能会重新审视该问题,这将不可避免地需要重大更改 - 请参阅 this PR

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      相关资源
      最近更新 更多