'how to set an array value in local.settings.json file in azure functions
When I try to insert an array as one of the Values in local.settings.json file:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": "",
"myArray": [
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value3",
"key2": "value4"
}
]
},
"ConnectionStrings": {
"SQLConnectionString": "myConnectionString"
}
}
I start getting exceptions. Can arrays be used in the local.settings.json
file? And if they can, what's the correct format?
Solution 1:[1]
You need to use the array indexer format.
E.g.
"myArray:[0]:key1": "value1",
"myArray:[0]:key2": "value2",
"myArray:[1]:key1": "value3",
"myArray:[1]:key2": "value4"
Solution 2:[2]
Add the array like string in local.settings.json file:
"myArray": "[
{
\"key1\": \"value1\",
\"key2\": \"value2\"
},
{
\"key1\": \"value3\",
\"key2\": \"value4\"
}
]"
Then deserialize it in your code as below:
string value = Environment.GetEnvironmentVariable("myArray");
objList = JsonConvert.DeserializeObject<List<object>>(value);
Solution 3:[3]
This is now possible!
My local settings file looks like this:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
},
"CustomArray": [
{
"CustomProp1": "foo0",
"CustomProp2": "bar0"
},
{
"CustomProp1": "foo1",
"CustomProp2": "bar1"
}
]
}
My config when deployed to Azure looks like this:
Then, to resolve the array in my Startup, I have something like this:
var settings = new List<CustomSetting>();
config.GetSection("CustomArray").Bind(settings);
... where CustomSetting
is a class like this:
public class CustomSetting
{
public string CustomProp1 { get; set; }
public string CustomProp2 { get; set; }
}
I am using version 3.1.5 of the Microsoft.Extensions.Configuration
Nuget package, and version 3.0.7 of the Microsoft.NET.Sdk.Functions
Azure Functions SDK.
Solution 4:[4]
Can arrays be used in the local.settings.json file? And if they can, what's the correct format?
It seems that arrarys is not supported currently in the local.settings.json.
As far as I know Values collection is expected to be a Dictionary, if it contains any non-string values, it will cause Azure function can not read values from local.settings.json
Based on my test, if it contains it will return null value
"myArray": [
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value3",
"key2": "value4"
}
]
Solution 5:[5]
I found a solution for this without making it complex you can simply create an array as follows which works perfectly inside the local.settings.json file in Azure Functions -
"myArray": "[\"Value1\",\"Value2\"]"
Solution 6:[6]
If you're trying to set an array in the local.settings.json file / environment variables, you can specify the following format:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"MyArray:0:Name": "Test",
"MyArray:1:Name": "Test2",
"MyArray:2:Name": "Test3"
}
}
In Startup.cs you can use the following syntax to bind the array
var settings = new List<MyArrayType>();
configuration.GetSection("MyArray").Bind(settings);
This is an example with .NET 6.0 and Azure Functions v4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Umair |
Solution 2 | Mitch Wheat |
Solution 3 | Community |
Solution 4 | Tom Sun - MSFT |
Solution 5 | Dileepa Rajapaksa |
Solution 6 | Scace |