Flatten Json for Azure Service App Configuration

The Azure CLI and Az Powershell module will let you import your .NET application's configuration (appSettings.json) to your azure Service/Web App or Azure Function.
Import
az webapp config appsettings set -g MyResourceGroup -n MyUniqueApp --settings @moreSettings.json
Export
az webapp config appsettings list --name MyWebapp --resource-group MyResourceGroup --subscription MySubscription
With Azure Functions, the importing and exporting can be achieved through the azure-functions-core tools.
Flatten-Json
There are times when I've needed to convert some JSON to the Key-Value syntax used in Application Configuration in Azure:
{
"SomeConfig":
{
"ApplicationName": "Your_App",
"Version": 1.2.3
},
"SomeOtherConfig":
{
"Test": 456
},
"MoreConfigs":[1, 2, 3]
}
This would translate to:
"SomeConfig__ApplicationName" = "Your_App"
"SomeConfig__Version" = "1.2.3"
"SomeOtherConfig__Test" = "456"
"MoreConfigs__0" = "1"
"MoreConfigs__1" = "2"
"MoreConfigs__2" = "3"
The dots (".") are replaced by underscores ("_") and colons (":") are replaced by double-underscores ("__").
Here is a gist for Powershell functions to flatten JSON:
Example appsettings.json
{
"Position": {
"Title": "Editor",
"Name": "Joe Smith"
},
"MyKey": "My appsettings.json Value",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
. /Flatten-Json.ps1
$json = Get-Content '.\appSettings.json' -Raw | ConvertFrom-Json
Flatten-Json $json
Echo out the hashtable without truncation:Flatten-Json $json | Format-Table -AutoSize
Name Value
---- -----
MyKey My appsettings.json Value
AllowedHosts *
Logging__LogLevel__Default Information
Position__Title Editor
Position__Name Joe Smith
Logging__LogLevel__Microsoft Warning
Logging__LogLevel__Microsoft.Hosting.Lifetime Information
TODO: Unflatten back to JSON
References
https://docs.microsoft.com/en-us/cli/azure/webapp/config/appsettings?view=azure-cli-latest
https://docs.microsoft.com/en-us/azure/azure-app-configuration/scripts/cli-import
https://docs.microsoft.com/en-us/powershell/module/azurerm.websites/set-azurermwebapp?view=azurermps-6.13.0&viewFallbackFrom=azurermps-5.6.0
https://blog.runasync.net/2018/04/11/manage-azure-web-app-application-settings-using-azure-powershell/




