JSON Config Variables Dynamic Arrays

operations
(Ben Guenter) #1

Hi there, I’m trying to substitute an array into JSON through the Octopus Variable. I’m seeing that Arrays are supported but it seems like it won’t substitute a group of values as an array.
This is what I’m trying to do.


So that when it replaces this in appsettings.
image
It should turn out like this.
image
But it gives me this error.

(Daniel Fischer) #3

Hi Ben,

Thanks for getting in touch! I’m very sorry for the delay in getting back to you. I have some helpful information here though.

Your approach is close but not quite there. I managed to get this working on my VM by doing the following:

Variable Value
AllowedOrigins [“Value1”,“Value2”]

The JSON file I used to test this looked like this:

{
  "AllowedOrigins": [
    "Replace me"
  ]
}

Octopus was able to successfully transform this file into the following:

{
  "AllowedOrigins": [
    "Value1",
    "Value2"
  ]
}

So I believe you are a nearly there. Your variable values need to be encapsulated with double quotes, square brackets and comma separated.

Let me know if this helps or if you have any further questions, and again I am sorry for the delayed response.

Best regards,
Daniel

(Alex Beynenson) #4

A note about an edge case I discovered that is not explicitly mentioned in the documentation. The replaced value MUST be an array already, otherwise JSON Variable Replacement replaces the variable as a string.

I originally had:

{
  "AllowedOrigins": null
}

And variable:

AllowedOrigins: ["Value1", "Value2"]

The replaced file ended up with:

{
  "AllowedOrigins": "[\"Value1\", \"Value2\"]"
}

The workaround was to explicitly “declare” the empty array:

{
  "AllowedOrigins": []
}

This is a bit unfortunate since the consuming code may have different interpretations of an empty array versus a missing array.