JSON Configuration - Can it insert into an array?

Hi Octopus

I have a scenario where I deploy my app with an appsettings.json file. Using the JSON configuration step to transform the file.

Inside the file, is an array of objects. Like:

`Things [ {Property1: "value", Property2: "value"}]`

I can replace these using the usual syntax in my octopus variables

`Things:0:Property1 = "replaced-value"`

My list though is actually a bit more dynamic, and sometimes I want to add an object to the array.

Am I right that the octopus process will only replace? If I referenced an object with index 2, would it be able to insert into the list?

Currently I think I have to checkin a code change to the appsettings.json file first to add an placeholder to the array so that octopus can replace values.

Thanks

1 Like

Hi Chris,

Thanks for getting in touch!

As long as the array already exists in the file, Octopus should be able to add additional values without a problem.
For example:
{"Authentication":{"BusinessUsers":[],"MonitoringUsers":[],},}

Can be added to with Octopus Variables:

Authentication:BusinessUsers:0  = value
Authentication:MonitoringUsers:0  = value

I hope this helps, let me know if you run into any problems with this.

Best regards,
Paul

Hi Paul

I just tried it and it didn’t seem to work. Maybe the fact that I have nested arrays is an issue. My real structure looks like

 "BasicAuthentication": {
    "Credentials": [
      {
        "Username": "username1",
        "Password": "password1",
        "Claims": [
          {
            "Type": "roles",
            "Value": "Read.All"
          },
          {
            "Type": "roles",
            "Value": "Write.All"
          }
        ]
      },
      {
        "Username": "username2",
        "Password": "password2",
        "Claims": [
          {
            "Type": "roles",
            "Value": "Read.All"
          },
          {
            "Type": "roles",
            "Value": "Write.All"
          }
        ]
      }
    ]
  }

I tried defining three variables:
BasicAuthentication:Credentials:2:Username = username3
BasicAuthentication:Credentials:2:Password = password3
BasicAuthentication:Credentials:2:Claims = [{“Type”: “roles”,“Value”: “Read.All”},{“Type”: “roles”,“Value”: “Write.All”}]

The deployed appsettings has no third entry created.

thanks

1 Like

Hi Chris,

It looks like you were right initially. After a chat with our engineers, what looked to me like it was creating entries was in fact just a replace that appeared that way.

So, as you first suggested, Octopus is only able to replace existing values in this way.

Apologies for the confusion.

You might be able to achieve the through the use of Configuration Transforms. It may not be as flexible as you need, but could allow you to have different templates based on environments or tenants perhaps.

Regards,
Paul

thanks for checking. Shame, would be a nice feature for us

Hi Chris,

I’ve had a look through our uservoice page and couldn’t see a similar suggestion, so feel free to add an entry on there to raise visibility on this.

Best regards,
Paul