Add new key value pairs for json using powershell

前端 未结 1 513
心在旅途
心在旅途 2021-01-21 11:12

I am creating an arm template to deploy data sets in ADF, for that i need to update an existing json file with new key value pairs based on my input file. how do i add new key v

1条回答
  •  生来不讨喜
    2021-01-21 11:54

    You don't need Add-Member, you simply need to "append" to the existing array in .properties.structure (technically, you're creating a new array that includes the new elements).

    Here's a simplified example:

    # Sample JSON.
    $json = @'
    {
        "name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
        "properties": {
            "type": "AzureDataLakeStoreFile",
            "structure": [
                {
                    "name": "VIN",
                    "type": "String"
                },
                {
                    "name": "MAKE",
                    "type": "String"
                }
            ],
        }
    }
    '@
    
    # Convert from JSON to a nested custom object.
    $obj = $json | ConvertFrom-Json
    
    # Append new objects to the array.
    $obj.properties.structure += [pscustomobject] @{ name = 'newname1' },
                                 [pscustomobject] @{ name = 'newname2' }
    
    # Convert back to JSON.
    $obj | ConvertTo-Json -Depth 3
    

    The above yields:

    {
      "name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
      "properties": {
        "type": "AzureDataLakeStoreFile",
        "structure": [
          {
            "name": "VIN",
            "type": "String"
          },
          {
            "name": "MAKE",
            "type": "String"
          },
          {
            "name": "newname1"
          },
          {
            "name": "newname2"
          }
        ]
      }
    }
    

    0 讨论(0)
提交回复
热议问题