使用 Powershell 替换 JSON 文件中的值

Replace a value in a JSON file using Powershell

如何用配置文件 (JSON) 中的另一个值替换 JSON 文件中的值? 这是 JSON 个文件的代码:

Config.json

{
    "ABlob_AAD_Snapshot": [
    {
      "name": "properties.availability.frequency",
      "value": "$Frequency$"
    }]       
}

ABlob_AAD_Snapshot.json

{
  "name": "AzureBlobStore",
  "properties": {
                 "type": "AzureStorage",
                 "availability": {
                                 "frequency": "Value from Config.json file"
                                 }
                 }
}

我想做的是:

  1. 浏览 Config.json 文件并将 "name" 和 "value" 段的值存储在变量中。 "name" 段值是 ABlob_AAD_Snapshot.json 文件中的路径。
  2. 按照ABlob_AAD_Snapshot.json文件中的路径,将段"frequency"替换为段"value"的值($frequency$)

完成此过程后,ABlob_AAD_Snapshot.json 应如下所示:

{
  "name": "AzureBlobStore",
  "properties": {
                 "type": "AzureStorage",
                 "availability": {
                                 "frequency": "$frequency$"
                                 }
                 }
}

这里的问题是我原来的config.json文件有多个数组(表示文件名)所以我会解析多个文件,"name"段的值并不总是相同 我的意思是,在这种情况下,值(或路径)是 properties.availability.frequency 但它可能是 properties.activities.scheduler.intervalproperties.activities.typeProperties.extendedProperties.WebAppClientID.

因此,如您所见,"nodes" 的名称和数量可能会发生变化。

这是我的 PowerShell 脚本:

    $ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition 

#path to config file
$ConfigFile = "$ScriptPath\ParameterConfigOriginal.json"

#Convert the json file to PSObject
$json = Get-Content $ConfigFile | Out-String | ConvertFrom-Json


#Get all the arrays (files) in Conifg.json
$files = $json | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name

#Go through all the arrays (files)
Foreach($file in $files)
{
   if( $file -eq '$schema') {continue}

    #store the path of the file to be modified
    $FileName = $file + ".json"
    $FilePath = "$ScriptPath\LinkedServices\" + $FileName"

   #Go through all the elements of the arrray
   Foreach($item in $json.$file)
   {
     #Store the path 
     $name = $item.name


    #Store the value
    $value = $item.value

    #Convert the file to be modified to PSObject
    $file = Get-Content $FilePath | Out-String | ConvertFrom-Json  

    #======STUCK IN HERE=============
    # How can dynamically navigate through the file nodes like this?

    $file.properties.availability.frequency

    #and set the corresponding value

    $file.properties.availability.frequency = $value

   }
}

我是 PowerShell 世界的新手,我不知道是否有 cmdlet 可以帮助我完成我需要的事情。

如有任何建议,我们将不胜感激。

编辑

简单路径

$snapshot.properties.availability.frequency

带数组的路径

$snapshot.properties.activities[0].scheduler.frequency

这是包含数组的 JSON 文件的示例 Destination file

这是结果 Destination file updated

知道会发生什么吗?

Invoke-Expression 会帮助你。

#Go through all the arrays (files)
Foreach($file in $files)
{
    $snapshot = (Get-Content ("./" + $file + ".json") | ConvertFrom-Json)

    # get config corresponds to the $file
    $config = Invoke-Expression ('$json.' + $file)

    # set value according to the config
    Invoke-Expression ('$snapshot.' + $config.name + "='" + $config.value + "'")

    # $snapshot.properties.availability.frequency
    # -> $Frequency$
}

编辑

您必须使用 ConvertTo-Json -Depth 100 将结果正确写入 JSON 文件(根据您的 JSON 文件指定适当的深度。)。 如果没有 -Depth 选项,您将得到类似 "@{type=Copy, typeProperties=;, ...}".

的结果