Добавьте новые пары ключ-значение для json, используя powershell - PullRequest
0 голосов
/ 13 ноября 2018

Я создаю шаблон arm для развертывания наборов данных в ADF, для этого мне нужно обновить существующий файл json новыми парами ключ-значение на основе моего входного файла.Как добавить новые пары ключ-значение в файл JSON с помощью PowerShell.Любая помощь в этом действительно приветствуется ..

Если я использую «Add-Member», он обновляется с новыми «ключом» и «значением» для всех свойств в структуре, как показано ниже. Но я хочу новый ключ изначение, которое будет добавлено после другой пары значений, как я показал в далеком коде, выделенном «Необходимо добавить это»

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

                },

Мой код должен выглядеть примерно так: «Необходимо добавить это»это пары ключ-значение, которые я собираюсь добавить в a для каждого цикла, если у меня есть входные данные из другого текстового файла.

    {
        "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 Ответ

0 голосов
/ 13 ноября 2018

Вам не нужно Add-Member, вам просто нужно «добавить» существующий массив в .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"
      }
    ]
  }
}
...