How to convert AppSettings.json to Advance Azure Settings with C#?
Asked Answered
C

4

6

Did you notice that Azure upgraded the AppSettings Management? Now it s possible to update multiple AppSettings in one shot using the Advanced Edit Options, but the formating is not the same as AppSettings.json.

Azure Edit Options

I m looking for a quick solution to convert my AppSettings section to Azure Advance Edit options format. Do you know how to do that?

So this:

"Simulation": {
    "ApiUrl": "YourApiUrl",
    "ApiKey": "YouApiKey",
    "Groups": [
        {
            "Name": "YourGroup",
            "Latitude": 45.50884,
            "Longitude": -73.58781,
            "Radius": 500
        }
    ],
    "Planifications": [
        {
            "GroupName": "YourGroup",
            "At": "07:00",
            "Status": 10
        }
    ]
}

will be formatted like:

[
  {
    "Name": "Simulation:ApiUrl",
    "Value": "YourApiUrl",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:ApiKey",
    "Value": "YourApiKey",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Name",
    "Value": "YourGroup",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Latitude",
    "Value": "45.50884",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Longitude",
    "Value": "-73.58781",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Radius",
    "Value": "500",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Planifications:0:GroupName",
    "Value": "YourGroup",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Planifications:0:At",
    "Value": "07:00:00",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Planifications:0:Status",
    "Value": "10",
    "SlotSetting": false
  }
]
Castra answered 1/8, 2019 at 19:2 Comment(1)
please check this ec2-13-235-104-60.ap-south-1.compute.amazonaws.comSycophant
S
21

There is a dotnet tool can do the conversion on Nuget dotnet-appsettings. It is required .NET 3.1 SDK.

dotnet tool install --global dotnet-appsettings
dotnet tool list --global
Usage: appsettings [appsettings.json]
Shier answered 14/6, 2021 at 16:13 Comment(5)
Never heard about it before your post! ThanksCastra
@CedricArnould We just recently implemented the tool using AspNetCore Configuration extension library. Because we need the tool for slow migration to Azure and docker.Shier
Cool! I updated the correct answer using yours.Castra
Is there an opposite of this? I mistakenly deleted my local appsetting.json and want values from Azure.Statecraft
@Statecraft No, as far as I know. Have to rebuild appsettings.json manually.Shier
C
1

Here is my quick solution to format my appsettings section to Azure Advance Edit options format:

My extensions:

    public static string ToJSON(this object obj)
    {
        return JToken.FromObject(obj).ToString();
    }

    public static IEnumerable<AzureSetting> ToAzureSettings(this object obj, string baseName = null, bool isEnumerable = false)
    {
        var t = obj.GetType();
        var result = new List<AzureSetting>();
        foreach (var propertyInfo in t.GetProperties())
        {
            var propValue = propertyInfo.GetValue(obj, null);
            if (propertyInfo.PropertyType.IsPrimitive
                || propertyInfo.PropertyType == typeof(string)
                || propertyInfo.PropertyType == typeof(TimeSpan)
                || propertyInfo.PropertyType == typeof(DateTime)
                || propertyInfo.PropertyType == typeof(DateTime?)
                || propertyInfo.PropertyType == typeof(DateTimeOffset)
                || propertyInfo.PropertyType == typeof(DateTimeOffset?)
            )
            {
                result.Add(
                    new AzureSetting()
                    {
                        Name = $"{baseName}:{propertyInfo.Name}",
                        Value = $"{propValue}"
                    }
                );
            }
            else if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
            {
                var enumerable = (IEnumerable)propValue;
                var i = 0;
                foreach (object child in enumerable)
                {
                    result.AddRange(child.ToAzureSettings($"{baseName}{propertyInfo.Name}:{i}", true));
                    i++;
                }
            }
            else
            {
                result.AddRange(propValue.ToAzureSettings($"{baseName}{propertyInfo.Name}:"));
            }
        }
        return result;
    }

public class AzureSetting
{
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "value")]
    public string Value { get; set; }
    [JsonProperty(PropertyName = "slotSetting")]
    public bool SlotSetting { get; set; } = false;
}

in program.cs

                    var simulationSettings = services.GetRequiredService<IOptions<SimulationSettings>>();
                    var azureJsonSettings = simulationSettings.Value.ToAzureSettings("Simulation").ToJSON();

                    using (StreamWriter writer = File.CreateText("bin\\SimulationAzureSettings.txt"))
                    {
                        writer.WriteLine(azureJsonSettings);
                    }

I hope it will help other people :)

Let me know if you have any questions.

Castra answered 1/8, 2019 at 19:2 Comment(0)
C
0

Azure App Configuration supports data import and export operations. Use these operations to work with configuration data in bulk and exchange data between your App Configuration store and code project. For example, you can set up one App Configuration store for testing and another one for production. You can copy application settings between them so that you don't have to enter data twice.

More info here: https://learn.microsoft.com/en-us/azure/azure-app-configuration/howto-import-export-data?tabs=azure-portal

Crowns answered 8/10, 2022 at 14:17 Comment(0)
S
-2

Here is the quick solution and Great tool article to convert the .net core appSetting.json to Azure appSetting.json.

Sycophant answered 23/5, 2021 at 17:16 Comment(4)
Application setting contain some secrets, using online tool is not the good idea.Shier
@ThoHo yes but before using an online tool if our Application setting has secrets then we can replace them.Sycophant
link is invalid.Demurrage
@Demurrage please check out nowSycophant

© 2022 - 2025 — McMap. All rights reserved.