Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set an array value in local.settings.json file in azure functions

Tags:

c#

azure

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?

like image 872
muskan Avatar asked Oct 31 '17 00:10

muskan


People also ask

How can I run Azure functions locally?

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?

Where can I read and write to JSON in Azure Data flow?

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.

How do I read environment variables in Azure App Settings?

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.

How are application settings applied to JSON files?

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.


4 Answers

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"

like image 74
Umair Avatar answered Oct 12 '22 20:10

Umair


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);
like image 20
muskan Avatar answered Oct 12 '22 20:10

muskan


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: Azure Function Settings Screenshot

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.

like image 6
NeilD Avatar answered Oct 12 '22 20:10

NeilD


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"
      }
    ]

enter image description here

like image 3
Tom Sun - MSFT Avatar answered Oct 12 '22 22:10

Tom Sun - MSFT