Use Azure DevOps Library to update variables in .json file using FileTransform@2

I’m trying to update my json file using FileTransform@2 by using variables from the Azure DevOps Library to populate these, but I am just getting an error that the value is already there.

##[error]Failed to apply JSON variable substitution. Changes are already present in the package.

This is part of my YAML file:

stages:
  - stage: UpdateSettingsFile
    displayName: 'Update settings file with ADO values'
    jobs:
      - job: UpdateSettings
        steps:
          - task: FileTransform@2
            displayName: 'Transform JSON Configuration'
            inputs:
              folderPath: '**/'
              fileType: 'json'
              jsonTargetFiles: '**/${{ parameters.powerPlatformEnvironment }}-deployment-settings.json'
              JSONVariableSubstitution: true 
              Overwrite: true  # Overwrite the existing file

and this is an example of my .json file

{
  "EnvironmentVariables": [
      {
          "SchemaName": "firstEmail",
          "Value": "$(firstEmail)"
      }
]
}

Should the value be left blank? How can i get around this?

The values should be updating in the .json file to import to Power Platform

1

You can use extension Replace Tokens, which can replace tokens in text based files with variable value. For example,

my.json:

{
  "EnvironmentVariables": [
      {
          "SchemaName": "firstEmail",
          "Value": "$(firstEmail)"
      }
]
}

YAML file:

variables:
- group: Test

steps:
  - task: replacetokens@6
    inputs:
      sources: 'my.json'
      tokenPattern: 'azpipelines'
  - task: CmdLine@2
    inputs:
      script: 'cat my.json'

Variable Group:

Result:


Update:

If you can only use FileTransform@2 task,

  1. Change your variable defined in variable group as Json format. See the details as mentioned by @Bright Ran-MSFT. In your example, there is one object, so the variable name looks like below.

  2. Format your FileTransform@2 task. When you want to replace tokens in JSON file, only folderPath and jsonTargetFiles are needed. This task doesn’t have parameters fileType, JSONVariableSubstitution and Overwrite. See the details about this task from FileTransform@2 – File transform v2 task.

2

The reason of the issue is that you defined the variable with wrong name. To use the FileTransform@2 task to perform variable substitution in JSON file, you need to must define the variable using JSONPath expressions.

See below example as reference:

  1. The sample of JSON file (config.json). And want to use the FileTransform@2 task to replace “[email protected]” and “[email protected]” with two new values.

    // config.json
    
    {
        "EnvironmentVariables": [
            {
                "SchemaName": "firstEmail",
                "Value": "[email protected]"
            },
            {
                "SchemaName": "secondEmail",
                "Value": "[email protected]"
            }
        ]
    }
    

  2. The sample of pipeline main YAML (azure-pipelines.yml).

    # azure-pipelines.yml
    
    # Define the pipeline variables with the new values to replace the old value in JSON file.
    # The names of variables must use JSONPath expressions.
    variables:
      EnvironmentVariables.0.Value: '[email protected]'
      EnvironmentVariables.1.Value: '[email protected]'
    
    steps:
    - bash: |
        cat config.json
      displayName: 'Print JSON before Variable substitution'
    
    - task: FileTransform@2
      displayName: 'Variable substitution for JSON'
      inputs:
        folderPath: ''
        xmlTransformationRules: ''
        jsonTargetFiles: 'config.json'
    
    - bash: |
        cat config.json
      displayName: 'Print JSON after Variable substitution'
    
  3. The result.

Related documentations:

  • FileTransform@2 – File transform v2 task
  • File and variable transform

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật