Json Variable Substitution


I’m having a problem with Json variable substitution.

I have a source controlled Json file that looks like:

{"Logging": {"RollingFile": {"LogLevel": 4 } } }

I want to add a new Log object to it using octopus and then configure it’s properties with variables.

So I’m replacing the json with a variable called Logging, with json like this:

{"Logging": {"RollingFile": {"LogLevel": 4 }, "SqlDB": {"LogLevel": 4 } } }

And then I have another property variable Logging:SqlDB:LogLevel, which I’m setting to say, 3.

The json gets replaced in the Logging variable, but the property variable does not seem to get applied.

So in my application the file looks like

{"Logging": {"RollingFile": {"LogLevel": 4 }, "SqlDB": {"LogLevel": 4 } } }

instead of

{"Logging": {"RollingFile": {"LogLevel": 4 }, "SqlDB": {"LogLevel": **3** } } }

The variables are in a variable set as shown in the attachment.

Thanks in advance

Hi Anton,

Thanks for reaching out! The problem with this approach is that we evaluate the JSON structure at the beginning of the process, and during the replacement you are changing the JSON structure once again. So by the time we try to modify Logging:SqlDB:LogLevel, Octopus still doesn’t know that new part of the JSON exists, because it wasn’t there when we initially loaded the JSON into memory.

Now, there’s an easy workaround for this using a variable within your variable:

  1. Change your variable Logging to be {"RollingFile": {"LogLevel": 4 }, "SqlDB": {"LogLevel": #{SQLDBLogLevel} } }
  2. Create a new variable called SQLDBLogLevel and assign the value 3 to it.

see attached screenshot for reference

Once this runs, you should end up with

{"RollingFile": {"LogLevel": 4 }, "SqlDB": {"LogLevel": 3} }

Let me know if that works.

Hi Dalmiro,

That worked great.

I thought I had tried that myself, but I think for some reason the variable I used cause the Json parsing to fail and the object not to get replaced.

Thank you

Glad to hear its working.