使用Powershell替换JSON文件中的值

时间:2017-11-08 18:19:15

标签: json powershell cmdlets

如何使用从配置文件(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$)的值
  3. 在此过程之后,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

    因此,您可以看到“节点”的名称和数量可能会发生变化。

    这是我的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

    对可能发生的事情有任何想法?

1 个答案:

答案 0 :(得分:1)

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=;, ...}"

之类的结果