0

I am trying to pass variable to release pipeline as array of objects. The goal is have in my appsettings.json something like

"myArrayObject": [
      {
        "host": "localhost:2948",
        "protocol": "http"
      }
    ]

In variable tab i created variable called myArrayObject and assigned to it

[{'host':'someUrl','protocol':'https'},{'host':'localhost:44394','protocol':'https'}]

somehow the variable was untouchted by release so then I added this script to pipeline

powershell -command "&{
 $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
 $json.myArrayObject = '$(myArrayObject)';
 $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json'; 
 }"

But then I got error

Missing type name after '['.
At line:1 char:129
+ ... llowedUris = [{'host':'someUrl','protoc ...
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token ':'someUrl'' in expression or 
statement.
At line:1 char:169
+ ... lowedUris = [{'host':'someUrl','protoco ...

Is there any way to achieve that ? to pass array of object as variable ?

I also tried this approach:

powershell -command "&{
 $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
 $json.myArrayObject = '[{"host":"someUrl","protocol":"https"},{"host":"localhost:44394","protocol":"https"}]';
 $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json'; 
 }"

but at the end i got :

"myArrayObject" : "[{host:someUrl,protocol:https},{host:localhost:44394,protocol:https}]"
kosnkov
  • 5,609
  • 13
  • 66
  • 107

1 Answers1

0

this is the solution:

powershell -command "&{
    $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
     $data = '$(additionalAllowedUris)' | ConvertFrom-Json;
     $json.additionalAllowedUris= $data.SyncRoot;
     $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
    }"

[{ \"host\" : \"someUrl\",\"protocol\" : \"https\"},{ \"host\":\"localhost:44394\",\"protocol\" : \"https\"},{ \"host\":\"someUrl2\",\"protocol\" : \"https\"}]
kosnkov
  • 5,609
  • 13
  • 66
  • 107
  • Great to see you have solved the issue. Please [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) once you can, this can be beneficial to other community members reading this thread. – Cece Dong - MSFT Jun 10 '20 at 01:34
  • i will but SO allows after 2 days – kosnkov Jun 10 '20 at 14:09