Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Editing a JSON with PowerShell

I have a json that looks something this

{
"ApiSettings": {
    "EnableSwagger": true,
    "UrlListeners": [
        "http://localhost:9000"
    ],
    "DebugMode":  true
},
}

And have some powershell that looks like this:

$UrlListeners = "http://(Ipofmymachine):9000"
$JsonFile = Get-Content $destinationDirectory\appsettings.json -raw | ConvertFrom-Json
$JsonFile.ApiSettings.UrlListeners = $UrlListeners
$JsonFile | ConvertTo-Json -Depth 9 | Set-Content $destinationDirectory\appsettings.json

The issue is that when the PowerShell is ran it converts the UrlListeners in the appsettings.json into a string, whereas it needs to stay as an array. Is there a way to force this value as an array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

ConvertFrom-Json and ConvertTo-Json doesn’t change UrlListeners to string, you do :).

Use this instead:
$JsonFile.ApiSettings.UrlListeners = @($UrlListeners)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading