Search code examples
jsonpowershellazure-rm-template

Add new key value pairs for json using powershell


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

Solution

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