Powershell and JSON – Part 2

We have a couple of standard JSON document which requires a property value to be updated based on the system they are moving to. So we started looking for an option which tool or scripting language can the best.

So, I looked to use Powershell. Here is an example of How a JSON document value can be updated.

This is how my JSON document content looks like

{
    "ConnectionStrings": {
        "DefaultConnection": "Server=SGT-SQL-UAT-01;Database=MyDb1;User Id=TestUser;Password=TestPswd;MultipleActiveResultSets=true"
    },
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        }
    }
}

Reading the JSON Document and updating the connection string values

[string] $file='G:\Dummy_Data\world_bank\connnection_string.json'

If( Test-Path -Path $file)
{   
   $data=(Get-Content  -Path $file -Raw)| ConvertFrom-Json   
    
           $data.ConnectionStrings.DefaultConnection="Server=SGT-SQL-UAT-01;Database=MyDb1;User Id=TestUser;Password=TestPswd;MultipleActiveResultSets=true"
           $data | ConvertTo-Json | Set-Content $file          
       
}

Leave a Reply

Your email address will not be published. Required fields are marked *