How to replace multiple values in json using variable substitue

I am trying to use “variable substitute in Json” plugin/feature to replace multiple values based on the role and environment defined in the scope for few of the variables.

Sample json file:

{
“name”:"#{name_key}",
“category”:"#{category_key}",
“automaticDateParsing”:true,
“multilineProcessingEnabled”:true,
“useAutolineMatching”:true,
“forceTimeZone”:false,
“filters”:[],
“cutoffRelativeTime”:"-10m",
“encoding”:“UTF-8”,
“fields”:{

},
"pathExpression":"#{path_key}",
"blacklist":[],
"sourceType":"LocalFile"

},

But when I run this step as part of process, none of the values are replaced with the variable defined. I have 3 variables defined for name_key, category_key, path_key but none of them is getting replaced in the json file.!

Screenshot attached: variables_screenshot|690x137

Hello,

Thank you for reaching out to us with your query about variable substitution.

There are two approaches to getting variables from Octopus into a JSON file - the first is to use structured configuration variables (recently renamed as it now handles more than just JSON) and the second is to substitute variables in templates.

It looks like the syntax in your example is from the substitute variables in templates feature but you mentioned that you’re using the structured configuration variables feature. You’ll need to use the substitute variables in templates feature for this work with your example syntax.

I hope this is helpful. Please let us know if you have any questions.

Best Regards,

Charles

Hi Charles,

Thanks for the prompt reply. I dont see template option of structured configuration variable feature in Octopus. Since I have 2 data subset in my json and I only want to substitute variables for only 1 of the subset, I am not sure how to pass the octopus variable name for the below source.

Sample json file:
{
“api.version”:“v1”,
“sources”:[{
name":"#{name_key}”,
category":"#{category_key}”,
“automaticDateParsing”:true,
“multilineProcessingEnabled”:true,
“useAutolineMatching”:true,
“forceTimeZone”:false,
“filters”:[],
“cutoffRelativeTime”:"-1h",
“encoding”:“UTF-8”,
“fields”:{},
pathExpression":"#{path_key}”,
“blacklist”:[],
“sourceType”:“LocalFile”
},
{
“name”:“testing”,
“category”:“test/testapp”,
“automaticDateParsing”:true,
“multilineProcessingEnabled”:true,
“useAutolineMatching”:true,
“forceTimeZone”:false,
“filters”:[],
“cutoffRelativeTime”:"-1h,
“encoding”:“UTF-8”,
“fields”:{},
“pathExpression”:“c:\test\test.log”,
“blacklist”:[],
“sourceType”:“LocalFile”
}]
}

The other thing to note is that I am not using structural variables template as it comes with IIS deployment and in my case, I am only having a step to substitute some variables in the json file located in my deployment server.

Thanks,
Dhawal

Hi Dhawal,

Thank you for getting back to me.

Would you be able to share a little more detail about how your deployment process is configured? In particular, is the JSON file deployed as part of a package or does it come from some other source?

Best Regards,

Charles

Hi Charles,

The json file is downloaded from Blob to the deployment target and then I am trying to substitute variables for that json file based on the environment and role defined in the scope in Project Variables as per screenshot attached .

.

I am looking to substitute some variables in the json file using the scope defined.

Thanks,
Dhawal

Hi Dhawal,

Thank you for getting back to me with the details.

I’m working on reproducing this locally so I can make a recommendation. I’ll get back to you once I have an update. In the meantime, could you let me know which version of Octopus server you are running?

Best Regards,

Charles

Hi Charles,

Thanks for looking into this. I am running Octopus 3.11.7 version.

Just FYI: The project variables that I am trying to substitute for those keys in the json file was already uploaded earlier.

Please do let me know if any additional details are required.

Thanks,
Dhawal

Hi Dhawal,

Thank you for getting back to me.

That’s a fairly old version of Octopus Deploy and it lacks some of the features of the newer versions that might be helpful with meeting your requirements. I’d recommend upgrading to a newer version if possible.

The newer versions of Octopus Deploy have the “Substitute variables in templates” feature available which I believe would solve this for you. You would be able to add a “Run a script” step, with a basic placeholder script as content, and use the variable substitution on the JSON file:

You can read more about how this works and find step by step instructions in the attached article (use the “Run a script” step in place of the “Deploy a package” step):

I hope this is helpful. Please let me know if you have any questions.

Best Regards,

Charles

Thanks Charles, I will check with my IT Admin to update octopus and try this template feature.

Appreciate your help on this!!

Thanks,
Dhawal

1 Like