2

I am creating powershell script which generation json file where i want to create json object runtime based on mentioned name in $env variable and put some value inside it as json.

$env="prd,test,dev,..,...." # there are total 15 env & $env has 1 and more env.

Desire JSON :

{
 
   "prd" : {
         "Sid": "xxxxxx" }
   "test" : {
         "Sid": "xxx" }

   "So on" : {}
}

Trying to write script:

$env="prd,test,..,...,.."

$env=$env - split ","
For ( $i = 0 $i -le ($env.length -1) ;$i+=1){

    $env[$i] = New-object System.Collection.ArrayList

    $env[$i].add("anydata10")

}

But this approach not working well since $env variable has any 1 or more env value.

In the powershell, can we create dynamic json object at runtime and any other approch to archive it ?

3
  • 2
    How does prd become prodsid1? Where did the o come from? :) Commented Apr 8, 2022 at 10:52
  • @MathiasR.Jessen No, value can be anything which get it from somewhere. Commented Apr 8, 2022 at 10:53
  • 1
    Why are you overwriting $env? There's no tax on variables. For the result you probably want to start with a new hash table entirely ($result = @{}) and build that up before feeding it to ConvertTo-Json. Nested hash tables work fine too: $result = @{}; $result.Add("prd", @{Sid="xxxxx"}); $result | ConvertTo-Json. Commented Apr 8, 2022 at 11:03

2 Answers 2

3

For the root-level object ({"prd":{...}, "test":{...}}), I'd suggest an ordered dictionary, then create a custom object with the Sid property for each property:

# prepare env keys
$environments = "prd,test,dev,stg,qa" -split ","

# create ordered dictionary
$jsonObject = [ordered]@{}

foreach($envName in $environments){
    # create 1 property, the value of which will be an object with a `Sid` property
    $jsonObject[$envName] = [pscustomobject]@{
        # resolve the correct `Sid` value from somewhere
        Sid = Get-SidForEnv $envName
    }
}

# convert the whole thing to json
$jsonObject |ConvertTo-Json -Depth 3

Based on your comments about getting the correct sid value "from somewhere", I assume you already have a function that translates prd to prodsid1 and so forth, so simply replace Get-SidForEnv with whatever function that is :)

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure I understand what you're trying to achieve. You can access environment variables using $env. So, if you have those values prd,test,... in some environment variable and you want to generate new environment variables based on those, you can do it like this:

#  $env:foo contains 'prd,test'
$env:foo -split ',' | % {
  New-Item -Path Env: -Name $_ -Value (@("anydata10") | ConvertTo-Json)  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.