Lostaunaum
Lostaunaum

Reputation: 705

Setting up a JSON object as a variable in a linux Powershell script

I am currently sending in a list of key value variables from octopus to my project into my prompt. These are read and set in my environment by the code below.

$testKeys = $OctopusParameters.keys | Where-Object {$_ -like "test.env.*"}
ForEach ($key in $testKeys)
{
    $value = $OctopusParameters[$key]
    $name = $key.subString(9) # remove test.env prefix
    $kubectlArgs += "--env=`"$name=$value`""
}

This is coming back as follows:

> --env="ASPNETCORE_ENVIRONMENT=integration" 
> --env="ConnectionStrings__MongoDB=my mongo connectgion string" 
> --env="ServiceConfiguration__MyJSONObjectConfiguration=[ {'MyID': 23, 'URL': 'http://google.com/', 'User': 'MyUser',
> 'Password': 'test', 'Domain': 'MyDomain', 'Key': 'myKey'} ]"

So as you can see, this works perfectly when it is a one to one, key = string value. The problem is that for the serviceConfiguration I am passing in a JSON object.

My code is grabbing the values I need like so

"ServiceConfiguration:MyJSONObjectConfiguration:0"

How do I configure me PS1 script to treat the serviceConfiguration as JSON object and not a string? Is it even possible?

Upvotes: 0

Views: 323

Answers (1)

Kory Gill
Kory Gill

Reputation: 7153

I am not sure you have provided enough information to solve your problem fully. One possibility is if the value starts with '[' then treat as JSON and use CovertFrom-Json.

Something like:

"[ {'MyID': 23, 'URL': 'http://google.com/', 'User': 'MyUser', 'Password': 'test', 'Domain': 'MyDomain', 'Key': 'myKey'} ]" | ConvertFrom-Json

Which gives an object like:

MyID     : 23
URL      : http://google.com/
User     : MyUser
Password : test
Domain   : MyDomain
Key      : myKey

Upvotes: 1

Related Questions