使用Powershell为JSON添加新的键值对 [英] Add new key value pairs for json using powershell
问题描述
我正在创建一个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屋!