使用 powershell 为 json 添加新的键值对 [英] Add new key value pairs for json using powershell

查看:24
本文介绍了使用 powershell 为 json 添加新的键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 value pairs to json file using powershell. Any help on this is really appreciated..

如果我使用Add-Member",它将使用新的键"和值"更新结构中的所有属性,如下所示.但我希望在另一个值对之后添加新的键和值,如我所示在下面的代码中突出显示需要添加这个"

If am using "Add-Member" it is updating with new "key" and "value" for all properties in the structure like below.but i want new key and value to be added after another value pair like i have shown in the far below code highlighted with "Need to add this"

                {
                    "name": "VIN",
                    "type": "String"
                    "newkey1" : "newvalue1"
                    "newkey2" : "newvalue2"
                },
                {
                    "name": "MAKE",
                    "type": "String"
                     "newkey1" : "newvalue1"
                    "newkey2" : "newvalue2"

                },

我的代码应该看起来像这样..需要添加这个"是我打算在每个循环中添加的键值对,只要我有来自另一个文本文件的输入.

My Code should look some thing like this.. "Need to add this" is the key value pairs i am intending to add in a for each loop as long as i have inputs from another text file.

    {
        "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,你只需要追加"到 .properties 中的现有数组.structure(从技术上讲,您正在创建一个包含新元素的新数组).

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).

这是一个简化的例子:

# 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"
      }
    ]
  }
}

这篇关于使用 powershell 为 json 添加新的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆