skip to Main Content

I am trying to implement an array of JSON objects in my local.settings.json file for my Azure function. The default file looks like this:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "Default",
    "AzureWebJobsDashboard": "Default",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  }

I know that any values added to the values section can be extracted in my .cs file using Environment.GetVariable(). However I would like to add an array like this:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "Default",
    "AzureWebJobsDashboard": "Default",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  },
  "data": [
    {
      "attribute" : "details"
      "attribute2": "details"
    },
    {
      "attribute" : "details"
      "attribute2": "details"
    }
  ]
}

I don’t think it’s possible to add this data array to the values section, since it’s expected to be a dictionary. How can I access the data array in my .cs file? I would like it to be in this format, due to the ease of adding objects when needed and the code will handle the changes with a loop since it is an array.

2

Answers


  1. Unfortunately, it can’t be done.
    Your best bet is to serialize the array as string and then to load it from config and deserialize it.

    Login or Signup to reply.
  2. With Azure Functions (I’ve tested with Isolated SDK but should be also working for In-Process SDK) you can define an array of objects and read it through the settings API in the following way.

    "ParentClientIdLookups:0:Key": "key1",
    "ParentClientIdLookups:0:Value": "a598b9a1-dcb9-4295-a480-1c17147c4b3b",
    "ParentClientIdLookups:1:Key": "key2",
    "ParentClientIdLookups:1:Value": "9446a26d-fc72-45bb-916d-c644dfc74f91",
    

    Read it upon startup:

    var listOfLookups = builder.Configuration.GetSection("ParentClientIdLookups")
                          .Get<ParentClientIdLookup[]>();
    

    ParentClientIdLookup definition:

    public class ParentClientIdLookup
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search