title: Replace a value in a JSON file using Powershell
body: <p>How can I replace a value in a JSON file with another value taken from a config file (JSON)?
  This is the code for the JSON files:</p>
  
  <p><strong>Config.json</strong></p>
  
  <pre><code>{
      "ABlob_AAD_Snapshot": [
      {
        "name": "properties.availability.frequency",
        "value": "$Frequency$"
      }]       
  }
  </code></pre>
  
  <p><strong>ABlob_AAD_Snapshot.json</strong></p>
  
  <pre><code>{
    "name": "AzureBlobStore",
    "properties": {
                   "type": "AzureStorage",
                   "availability": {
                                   "frequency": "Value from Config.json file"
                                   }
                   }
  }
  </code></pre>
  
  <p>What I'm trying to do is the following:</p>
  
  <ol>
  <li>Go through the Config.json file and store the values for "name" and "value" segments in variables. The "name" segment value is a path in ABlob_AAD_Snapshot.json file.</li>
  <li>Follow the path in the ABlob_AAD_Snapshot.json file and replace the segment "frequency" with the value of segment "value" (<code>$frequency$</code>)</li>
  </ol>
  
  <p>After this process, the ABlob_AAD_Snapshot.json should look like this:</p>
  
  <pre><code>{
    "name": "AzureBlobStore",
    "properties": {
                   "type": "AzureStorage",
                   "availability": {
                                   "frequency": "$frequency$"
                                   }
                   }
  }
  </code></pre>
  
  <p>The problem here is that my original config.json file has more than one array (which represents file names) so I will parse more than one file and the value for "name" segment will not always be the same I mean, in this case, the value (or path) is <code>properties.availability.frequency</code> but it could be <code>properties.activities.scheduler.interval</code> or <code>properties.activities.typeProperties.extendedProperties.WebAppClientID</code>.</p>
  
  <p>So as you can see the name and the quantity of "nodes" could change.</p>
  
  <p>This is my PowerShell script:</p>
  
  <pre><code>    $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
  
     }
  }
  </code></pre>
  
  <p>I'm new in the PowerShell world and I don't know if there is a cmdlet that helps me to do what I need.</p>
  
  <p>Any suggestion will be appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><em>Simple Path</em></p>
  
  <pre><code>$snapshot.properties.availability.frequency
  </code></pre>
  
  <p><em>Path with arrays</em></p>
  
  <pre><code>$snapshot.properties.activities[0].scheduler.frequency
  </code></pre>
  
  <p>this is an example of the JSON file with arrays
  <a href="https://i.stack.imgur.com/FqKEg.png" rel="nofollow noreferrer">Destination file</a></p>
  
  <p>and this is the result
  <a href="https://i.stack.imgur.com/fgfLL.png" rel="nofollow noreferrer">Destination file updated</a></p>
  
  <p>Any idea on what could be happening?</p>