Hierarchical JSON delete Values

Hello,
want to delete a specific key with octopus deploy in an appconfig.json

Example for my LOCAL- Enviroment .json:
“IotCoreRegistry”: {
“Uri”: {
“Scheme”: “https”,
“Host”: “registry.iotcore”,
“Port”: 443
},
“Service”: {
“ComponentId”: “Master/TestService”,
“ServiceId”: “Monitoring”,
“Protocol”: “tcp”
},
“Type”: “File”,
“File”: “C:\Server\log.txt”
},

But on my TEST - Enviroment I need the following Setup:
“IotCoreRegistry”: {
“Uri”: {
“Scheme”: “https”,
“Host”: “registry.iotcore”,
“Port”: 443
},
“Service”: {
“Protocol”: “tcp”
},
“UseFake”: false
}

Know my Questions? How can I delte some specific parameter with octopus … example: “Type”: “File” …

Regards
Alex

Hi Alex,

Thanks for getting in touch! Octopus does not have the ability to directly remove a key during the transformation/substitution. However there are some workarounds to this you can try.

One workaround would be to place the whole section inside a variable and substitute it based on environment. For example:

“IotCoreRegistry”: {
“Uri”: {
“Scheme”: “https”,
“Host”: “registry.iotcore”,
“Port”: 443
},
#{jsonSubstitutionVariable}

And in Octopus set a variable with the name jsonSubstitutionVariable scoped to LOCAL

“Service”: {
“ComponentId”: “Master/TestService”,
“ServiceId”: “Monitoring”,
“Protocol”: “tcp”
},
“Type”: “File”,
“File”: “C:\Server\log.txt”
},

With the below additional value on the same variable scoped to TEST

“Service”: {
“Protocol”: “tcp”
},
“UseFake”: false
}

From there you could just do a variable substitution against the file and achieve the same results.

You are even able to bind variables inside of your variable, for example, your value below could contain a nested variable with further scoping. (An alternative here would be to use the JSON substitution from our docs and not bother with the nested variable. Both would net the same result.)

“Service”: {
“ComponentId”: “Master/TestService”,
“ServiceId”: “Monitoring”,
“Protocol”: “tcp”
},
“Type”: “File”,
“File”: “#{FileURL}”
},

Does this look like it would suite your needs here?

Best regards,
Daniel

Hello Daniel,

thank you very much for you reply! Seem like a nice workaround the only thing which is not quite clear for me is how do i get the variable #{jsonSubstitutionVariable} inside my json section?

Regards
Alex

Hi Alex,

Thanks for getting back here. Are you unsure how to substitute this variable inside your JSON file?

If so, you would use our Substitute Variables in Files feature.

If you create a project variable in Octopus named jsonSubstitutionVariable (Or whatever you would like) Octopus will evaluate it inside the targeted file with the above feature.

When you build your JSON file before it is packaged and pushed to Octopus, you would simply replace the section of code (Which you now have as a variable within Octopus) with the Octopus variable syntax and the name of the variable: #{jsonSubstitutionVariable}

This way, octopus will deploy your file and replace the variable with the rest of the code the file requires depending on the scoping of the variable in Octopus.

This workaround is not the neatest, but accounts for the the fact that Octopus can not just remove the parameters from your JSON code.

Does that help?

Sorry if I have misinterpreted your question here. Let me know if you need any further help here at all. :slight_smile:

Best regards,
Daniel

Hmmmm, unfortunately still having the same problem. Maybe my questions was not quite clear.

My Problem is: How can I provide the variable inside my .json-File?
Do I have to manuelly add an extra variable for this subtituation?
Or is it somehow possible to insert that specific #{jsonSubstitutionVariable} at given position?

For me the only knowledge is that i can replace variables in json with octopus for example:

Vairablename: IotCoreRegistry:Uri:Port
Value: 80
–> Will replace the Port-Value-Pair with the value 80 …

But I understand your smart approach with a variable inside a of a variable :slight_smile:
Just not so clear for me how to get that “help”-variable inside my .json …

Regards
Alex

Hi Alex,

Thanks for getting back again. I may have misunderstood exactly where you were getting stuck here. Hopefully I’m understanding now and the below will provide a better explanation.

So it sounds like you are not entirely clear about the where and how you could get the #{jsonSubstitutionVariable} into your file. (As a note, this variable could be called anything and simply points to a variable in your Octopus Project Variables)

As a further note, the json substitution you are using in your example IotCoreRegistry:Uir:Port is a great way to substitute single values in your json file. However, this feature has no way to remove entire properties/lines such as “Type”: “File”,

Which is where this sort of workaround comes in.

So assuming you have your current file, as you initially described to me. appconfig.json

Example for my LOCAL- Enviroment .json:
“IotCoreRegistry”: {
“Uri”: {
“Scheme”: “https”,
“Host”: “registry.iotcore”,
“Port”: 443
},
“Service”: {
“ComponentId”: “Master/TestService”,
“ServiceId”: “Monitoring”,
“Protocol”: “tcp”
},
“Type”: “File”,
“File”: “C:\Server\log.txt”
},

To have Octopus remove lines from the above appconfig.json based on the environment you are deploy to, you can package your appconfig.json file without these lines at all. And instead, have it already pointing to a variable in your project.

So you could package your .json file to contain the following:

“IotCoreRegistry”: {
“Uri”: {
“Scheme”: “https”,
“Host”: “registry.iotcore”,
“Port”: 443
},
#{jsonSubstitutionVariable}

Then in Octopus you can create a single variable (again it can be named anything, just so long as it’s the same string as in the .json file.) which contains multiple values, pointing to the rest of your .json parameters which may be different depending on environment. In my example below one is scoped to Dev and the other Test.

When I use the “Substitute Variables in Files” feature I mentioned previously and point to my appsettings.json, Octopus will identify the variable syntax #{} and match it to your project variable based on variable name and scoping.

This is currently the best method we have for “removing/adding” lines in your .json file based on environment.

I hope I have done a better job of explaining this workaround idea and how you can get the variable into your JSON file.

If you are still stuck on this or have any further questions at all, please don’t hesitate to let me know.

Best regards,
Daniel

1 Like

Thank you very much !!! :slight_smile:

Hi Alex,

No worries at all. Feel free to get in touch at any time. :slight_smile:

Best regards,
Daniel

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