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?
Thankfully, we can use Azure Function Core Tools to create a local.settings.json file and import our Function settings to that file so we can run our Functions locally as if we were running it against that environment! Why would we do this?
Each file contains an array of objects. In mapping data flows, you can read and write to JSON format in the following data stores: Azure Blob Storage, Azure Data Lake Storage Gen1 and Azure Data Lake Storage Gen2, and you can read JSON format in Amazon S3. The below table lists the properties supported by a json source.
App settings can be read from environment variables both when developing locally and when running in Azure. When developing locally, app settings come from the Values collection in the local.settings.json file.
Application settings are applied in a very specific order, with the json files applying in the order that they are added. If there are duplicate settings, the existing files are overwritten.
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"
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);
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.
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"
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With