App Config - Import from JSON
App Config - Import from JSON

Azure App Configuration Service – Import JSON Settings from File

In this article, we are going to have a look at how to import the key-value pairs from a JSON file.

Prerequisite

For following all steps in this article, we will need Azure Subscription.  If you don’t have an Azure subscription, create an Azure free account before you begin.

Create Azure App Configuration Service

We will need an instance of Azure App Configuration Service. You can refer this article to create one using Azure Portal. Alternatively, you can run the bicep script from this article to create one for you. Refer my GitHub repository to create the resource if you want to use the Bicep files. This bicep file will also add some key value pairs.

Sample JSON File

Let’s say, for this discussion, we want to use the JSON from the code snippet given below.

Creating JSON Key-Values in Azure Portal

The bicep file provided in my GitHub repository inserts some key value pairs. The values in those pairs are simpler. What if we want to insert a JSON object as key-value pair ? Can we do that using Azure Portal ?

The answer is yes. We need to specify appropriate content type for the value. If the type is application and the subtype (or suffix) is json, the media type will be considered a valid JSON content type. Some examples of valid JSON content types are:

  • application/json
  • application/activity+json
  • application/vnd.foobar+json;charset=utf-8

So, now how to insert the JSON object from our sample JSON file as key-value pairs ? For every key, we need to insert the JSON Path of each setting (the tokens in hierarchy separated by colon character). Then we can specify the value of respective setting. Make sure that content type is selected to be application/json.

The inputs are shown in the table given below. The table assumes that we want tokens only from first 2 top hierarchies. That’s why, the value of Settings:Logging is a JSON object.

KeyValueContent Type
Settings:BackgroundColor“Green”application/json
Settings:FontSize24application/json
Settings:UseDefaultRoutingfalseapplication/json
Settings:BlockedUsersnullapplication/json
Settings:ReleaseDate“2020-08-04T12:34:56.789Z”application/json
Settings:RolloutPercentage[25,50,75,100]application/json
Settings:Logging{“Test”:{“Level”:”Debug”},”Prod”:{“Level”:”Warning”}}application/json
The Example is from Documentation

Importing the JSON File

We can use Azure CLI to import the contents from the JSON file. We can use az appconfig kv command. The command takes the JSON file as input. We can also specify the token separator to be : character. And we can also specify the depth parameter to specify how many maximum tokens, we want to be present in the key.

Below is the list of parameters we are going to need:

  • s or source, this can be either appconfig, appservice or file. We can use same command with different source value, in case we want to import key-values from another appconfig or app service.
  • format, this can be set to json, properties or yaml. As we want to import JSON file, we need to set it to value “json“.
  • path, to specify the JSON path file
  • content-type, to be application/json
  • separator, the delimiter for flattening the key-value pairs to json or yaml file. Supported values: ‘.’, ‘,’, ‘;’, ‘-‘, ‘_’, ‘__’, ‘/’, ‘:’.
  • depth, the depth for flattening the json or yaml file to key-value pairs. Flatten to the deepest level by default if –separator is provided. 
  • we also need to provide the app config store name or connection string so that command can know where the key-value pairs are required to be inserted

The command is shown in the code snippet given below.

Are you getting Forbidden error ?

Let’s see what happens when I run the commands mentioned above. Firstly, I ran the command using connection string command line parameter. I used the same connection string that I used in previous blog articles. I was getting a forbidden error as shown in the snapshot given below.

Azure CLI – Import Key-Value Pairs from JSON file – Causes Forbidden Error

I was scratching my head for couple of hours. I did not find much on internet about this issue. Later, I realized that I was using a read-only connection string. A read-only connection string is not permitted to make changes (as the name suggests that it is read-only).

So, I changed the connection string to read-write connection string and it worked like a charm !

Azure CLI – Import from JSON File – Worked as Expected

After successful execution of the command, I opened Azure Portal and navigated to Azure App Configuration instance. I could see all the new key-value pairs.

Azure Portal – The new key value pairs imported from a JSON file

You may not face this issue if you pass the name of app configuration service and you have all the necessary permissions. I hope you find this information helpful. Let me know your thoughts.

Leave a Reply Cancel reply