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,
-
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.
-
Format your
FileTransform@2
task. When you want to replace tokens in JSON file, onlyfolderPath
andjsonTargetFiles
are needed. This task doesn’t have parametersfileType
,JSONVariableSubstitution
andOverwrite
. 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:
-
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]" } ] }
-
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'
-
The result.
Related documentations:
- FileTransform@2 – File transform v2 task
- File and variable transform