Add new key value pairs for json using powershell

落爺英雄遲暮 提交于 2019-12-04 04:53:50

问题


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

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')]"
        ]
    },

回答1:


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


来源:https://stackoverflow.com/questions/53285048/add-new-key-value-pairs-for-json-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!