In this article, we are going to have a look at how to import the key-value pairs from a JSON file.
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:
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
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.
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
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
yaml. As we want to import JSON file, we need to set it to value “
- path, to specify the JSON path file
- content-type, to be
- separator, the delimiter for flattening the key-value pairs to json or yaml file. Supported values: ‘.’, ‘,’, ‘;’, ‘-‘, ‘_’, ‘__’, ‘/’, ‘:’.
- depth, the depth for flattening the
yamlfile 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.
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 !
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.
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.