Dynamically add extra value in JSON file

I have a project which runs on multiple environments. For this example, let’s take A and B as two of these environments.

One of the steps in the process is the JSON Configuration Variables step. In env A, the appsettings.json file where variable replacement is being done requires a particular key, but in env B, it does not. In other words, the json file needs to look like this:

  key1: "value1",
  key2: "value2",
  key3: "value3"     // env A has this, but env B does not

The issue is that the appsettings.json file that comes from the repository does not contain key3.

Is there a way that I can make Octopus insert “key3” there, along with its configured value, if I was running it in env A?

Unfortunately you have hit upon one of the limitations of the the json substitution feature in that it works well for direct substitution but not so much for transformation. If you want to change which properties are on an object, your best bet at the moment is to perform the modification yourself in-code. This is definitely not ideal and something that we are aware of and hoping to find a better solution for.

There is an open suggestion in uservoice that has some support behind it
and we may end up looking at something like https://github.com/Microsoft/json-document-transforms.

I’m sorry we don’t have a great answer for you for this problem at the moment but id be keen to hear if you think the json-document-transforms approach above would be suitable for your scenario.

Ah, that’s what I thought. No problem Robert, and thanks for your quick response!

Quick update here: we ended up making changes to the file contents at the source. Something like JSON transforms would definitely have been useful here.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.