gpt4 book ai didi

json - 使用powershell为json添加新的键值对

转载 作者:行者123 更新时间:2023-12-02 09:10:11 26 4
gpt4 key购买 nike

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

如果我使用“添加成员”,它将使用新的“键”和“值”更新结构中的所有属性,如下所示。但我希望在另一个值对之后添加新的键和值,如我所示在下面突出显示的代码中“需要添加此”

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

},

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

    {
"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.struct中的现有数组(从技术上讲,您正在创建一个包含新元素的新数组)。

这是一个简化的示例:

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

关于json - 使用powershell为json添加新的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com