In the appsetting.json, from one of the services at work, there is an array property that looks something like this
{
"SampleSettings": {
"ArrayProp": [
"A",
"B",
"C"
],
"StringProp": "ABC"
}
}
We recently have a new test environment which we want the ArrayProp to be empty. So I add a new appsettings.Test.json to override it, that looks like this
{
"SampleSettings": {
"ArrayProp": [],
"StringProp": "XYZ"
}
}
I was quite surprised to find that did not override and did not make ArrayProp empty. The ArrayProp still had 3 elements “A”, “B” and “C”. However, the StringProp did override and became “XYZ”. I then tried setting the ArrayProp to null but this also did not work. I also tried setting the whole SampleSettings to null but that did not work either.
After researching this issue for a while, it does not seem like there is a way to replace an entire array with one from another config file. However, each individual item can be overridden and more items can be added if needed. So to fix this, I set the ArrayProp to be an array containing empty strings, like this.
{
"SampleSettings": {
"ArrayProp": [
"",
"",
""
],
"StringProp": "XYZ"
}
}
This solves the problem for my scenario but you might want to make sure that your code handles empty values for this setting.
Alternatively, you can set all array properties to be empty in the base appsettings.json, and override them with the values that you need in each environment specific appsettings JSON file. For example
// appsettings.json
{
"SampleSettings": {
"ArrayProp": [
],
"StringProp": "ABC"
}
}
// appsettings.Dev.json
{
"SampleSettings": {
"ArrayProp": [
"A",
"B",
"C"
],
}
}
// appsettings.Test.json
{
"SampleSettings": {
"StringProp": "XYZ"
}
}