使用 powershell 为 json 添加新的键值对

Add new key value pairs for json using powershell

我正在创建一个 arm 模板来在 ADF 中部署数据集,为此我需要根据我的输入文件使用新的键值对更新现有的 json 文件。如何使用 powershell 将新的键值对添加到 json 文件。 非常感谢对此的任何帮助..

如果我正在使用 "Add-Member",它正在使用新的 "key" 和 "value" 更新结构中的所有属性,例如 below.but 我想添加新的键和值在另一个值对之后,就像我在下面用 "Need to add this"

突出显示的代码中显示的那样
                {
                    "name": "VIN",
                    "type": "String"
                    "newkey1" : "newvalue1"
                    "newkey2" : "newvalue2"
                },
                {
                    "name": "MAKE",
                    "type": "String"
                     "newkey1" : "newvalue1"
                    "newkey2" : "newvalue2"

                },

我的代码看起来应该是这样的。"Need to add this" 是我打算在 for each 循环中添加的键值对,只要我有来自另一个文本文件的输入。

    {
        "name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
        "type": "Microsoft.DataFactory/factories/datasets",
        "apiVersion": "2018-06-01",
        "properties": {
            "linkedServiceName": {
                "referenceName": "AzureDataLakeStore1",
                "type": "LinkedServiceReference"
            },
            "annotations": [],
            "type": "AzureDataLakeStoreFile",
            "structure": [
                {
                    "name": "VIN",
                    "type": "String"
                },
                {
                    "name": "MAKE",
                    "type": "String"
                },
                {
                    "Need to add this": "Need to add this",
                    "Need to add this": "Need to add this"
                },
                {
                    "Need to add this": "Need to add this",
                    "Need to add this": "Need to add this"
                },
                {
                    "Need to add this": "Need to add this",
                    "Need to add this": "Need to add this"
                },
                {
                    "Need to add this": "Need to add this",
                    "Need to add this": "Need to add this"
                }

            ],
            "typeProperties": {
                "format": {
                    "type": "TextFormat",
                    "columnDelimiter": "|",
                    "rowDelimiter": "\n",
                    "quoteChar": "\"",
                    "nullValue": "\"\"",
                    "encodingName": null,
                    "treatEmptyAsNull": true,
                    "skipLineCount": 0,
                    "firstRowAsHeader": false
                },
                "fileName": "[parameters('Veh_Obj_properties_typeProperties_fileName')]",
                "folderPath": "[parameters('Veh_Obj_properties_typeProperties_folderPath')]"
            }
        },
        "dependsOn": [
            "[concat(variables('factoryId'), '/linkedServices/AzureDataLakeStore1')]"
        ]
    },

您不需要 Add-Member,您只需要 "append" 到 .properties.structure 中的现有数组(从技术上讲,您正在创建一个包含新元素的新数组).

这是一个简化的例子:

# 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

以上结果:

{
  "name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
  "properties": {
    "type": "AzureDataLakeStoreFile",
    "structure": [
      {
        "name": "VIN",
        "type": "String"
      },
      {
        "name": "MAKE",
        "type": "String"
      },
      {
        "name": "newname1"
      },
      {
        "name": "newname2"
      }
    ]
  }
}
$CompanyJSON = @"
{
    "name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
    "type": "Microsoft.DataFactory/factories/datasets",
    "apiVersion": "2018-06-01",
    "properties": {
        "linkedServiceName": {
            "referenceName": "AzureDataLakeStore1",
            "type": "LinkedServiceReference"
        },
        "annotations": [],
        "type": "AzureDataLakeStoreFile",
        "structure": [
            {
                "name": "VIN",
                "type": "String"
            },
            {
                "name": "MAKE",
                "type": "String"
            }

        ],
        "typeProperties": {
            "format": {
                "type": "TextFormat",
                "columnDelimiter": "|",
                "rowDelimiter": "\n",
                "quoteChar": "\"",
                "nullValue": "\"\"",
                "encodingName": null,
                "treatEmptyAsNull": true,
                "skipLineCount": 0,
                "firstRowAsHeader": false
            },
            "fileName": "[parameters('Veh_Obj_properties_typeProperties_fileName')]",
            "folderPath": "[parameters('Veh_Obj_properties_typeProperties_folderPath')]"
        }
    },
    "dependsOn": [
        "[concat(variables('factoryId'), '/linkedServices/AzureDataLakeStore1')]"
    ]
}
"@
$AddArray = @('' +
'{' +
'    "name": "VIN",' +
'    "type": "String",' +
'    "newkey1": "newvalue1",' +
'    "newkey2": "newvalue2"' +
'}';
'{' +
'    "name": "MAKE",' +
'    "type": "String",' +
'    "newkey1": "newvalue1",' +
'    "newkey2": "newvalue2"' +
'}'
)
Add-Type -AssemblyName System.Web.Extensions;
$json = [System.Web.Script.Serialization.JavaScriptSerializer]::new();
$json.MaxJsonLength = 2147483647; #max integer or less
$CompanyObj=$json.Deserialize($CompanyJSON, [System.Object]);
foreach ($chank in $AddArray){
    $CompanyObj.properties.structure+=$json.Deserialize($chank, [System.Object]);
}
$CompanyJSON=$json.Serialize($CompanyObj);
Write-Host $CompanyJSON

您会看到 \u0027 等。它是 unicode。 JSON会明白的