Azure Devops YAML File Transformation Task for JSON File End to End
Published: 2022-07-05 10:50:09

---
title: Azure Devops YAML File Transformation Task for JSON File End to End
published: true
date: 2021-08-06 13:34:56 UTC
tags: 
canonical_url: https://lkgforit.com/azure-devops-yaml-file-transformation-task-for-json-file-end-to-end
---

In this article we will see how to do appsettings json file transformation using Azure Devops YAML pipelines. In this article we will use _FileTransformationTask@1_ in YAML pipeline - [reference](https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/file-transform?view=azure-devops).

Video explanation

[https://youtu.be/\_iPfzH3ENAk](https://youtu.be/_iPfzH3ENAk)

We need to update the appsettings.json file on any dotnet core app deployment so that the configurations need to be updated as per environment. Even we use the environment json files for angular or react or any NodeJS apps so that we can configure values in the configuration json file with environment specific values using the automated CD pipeline using YAML.

Here we can see how to do that using the File transformation task in YAML Azure Devops pipelines.

```
# File transform
# Replace tokens with variable values in XML or JSON configuration files
- task: FileTransform@1
  inputs:
    #folderPath: '$(System.DefaultWorkingDirectory)/**/*.zip' 
    #enableXmlTransform: # Optional
    #xmlTransformationRules: '-transform **\*.Release.config -xml** \*.config-transform **\*.$(Environment.Name).config -xml**\*.config' # Optional
    #fileType: # Optional. Options: xml, json
    #targetFiles: # Optional

```

Above is the syntax for this yaml task, in which folderpath is the path of the solution or application folder in the repo, than if you want to xml transform then we have to set true in enableXmlTransform field, but in this example we are going to cover on json file transformation so we will leave this field which is optional. We will also leave xmlTransformationRules field which is also optional and only for xml transformation. In fileType field give json as value. In targetFiles field provide the json file path directly or relatively, so if you have multiple appsettings files inside folder path you have given in first field than giving relative path will see for all the appsettings json file like this: _'\*\*/appsettings.json'_.

```
  # Update appsettings.json via FileTransform task.
- task: FileTransform@1
  displayName: 'File transformation: appsettings.json'
  inputs:
    folderPath: '${{ parameters.sourceDirectory }}'
    targetFiles: '**/appsettings.json'
    fileType: json

```

Ok the main part comes here, here we have updated the filetransform task, now how to update the values inside the appsettings json file with the environment specific configurations, lets see that now.

This file transform task will update the fields inside the appsettings json file from the available variables based on name of variable matching with the field inside the appsettings json file. For example, take the below json file as our app settings json file:

```
{
    "customFunctions": [],
    "importedLibraries": [],
    "firstResource": "",
    "groupOne": {
        "firstFieldInOne": "dev",
        "name": "sampleApp"
    },
    "groupTwo": {
        "nameTwo": "dummyValueTwo",
        "idTwo": ""
    },
    "runtimeSettings": {
        "storage": "",
        "telemetry": {
            "instrumentationKey": ""
        }
    }
}

```

So, in this appsettings file if you want to replace the parent elements of the json file then you should add the variables inside the yaml pipeline or as a pipeline variable with the name of the element - for example, to update 'firstResource' field you need to create a variable with name _firstResource_.

If you want to update inner child elements than you have to create variables with their path separated values by dot (.), here i mean there path values as element for which value need to be updated in second level child means the parent elements should be mentioned with dot(.) separated names, for this example, to update _firstFieldInOne_ field which is a child of element _groupOne_ we have to create a variable with name :

> groupOne.firstFieldInOne

So, for above two values update we must create following variables in the yaml or in the pipeline variables:

```
variables:
  firstResource: "firstResourceValue"
  groupOne.firstFieldInOne: "FirstField Value"

```

So, for updating all the values in the above sample appsettings json file or any json configuration file we must follow this rule and create following variables:

```
variables:
  firstResource: "firstResourceValue"
  groupOne.firstFieldInOne: "FirstField Value"
  groupOne.name: "FirstField name"
  groupTwo.nameTwo: "name two Value"
  groupTwo.idTwo: "id two Value"
  runtimeSettings.storage: "storage account Value"
  runtimeSettings.telemetry.instrumentationKey: "instrumentation key"

```

If you use the above file transformation task and create these variables in the pipeline then you will see the following as the output json file which you can place in the deployment environment.

```
{
    "customFunctions": [],
    "importedLibraries": [],
    "firstResource": "firstResourceValue",
    "groupOne": {
        "firstFieldInOne": "FirstField Value",
        "name": "FirstField name"
    },
    "groupTwo": {
        "nameTwo": "name two Value",
        "idTwo": "id two Value"
    },
    "runtimeSettings": {
        "storage": "storage account Value",
        "telemetry": {
            "instrumentationKey": "instrumentation key"
        }
    }
}

```

Happy Coding :)

Refer videos in my channel for live explanations.

[https://youtu.be/QxJ\_y6pV6No](https://youtu.be/QxJ_y6pV6No)

[https://youtu.be/dGLWBWZavhw](https://youtu.be/dGLWBWZavhw)

[https://youtu.be/C8t4EUV6yok](https://youtu.be/C8t4EUV6yok)

YouTube Channel

[https://www.youtube.com/channel/UCnYzqHl\_und412jN2RTjJVA](https://www.youtube.com/channel/UCnYzqHl_und412jN2RTjJVA)