Replace JSON Config
Replace JSON Config is a free Azure DevOps Pipeline task that recursively searches JSON configuration files and replaces destination values using key-value pairs from a source JSON file.
Features
- Recursively searches destination folders and subfolders.
- Supports configurable JSON file filters.
- Reads key-value pairs from a source JSON file.
- Matches keys between the source and destination JSON files.
- Replaces destination values with corresponding source values.
- Supports nested JSON objects in destination files.
- Supports placeholders such as
$(ApiUrl) and $(DatabaseString).
- Can be used in Azure DevOps Build and Release pipelines.
- Works with Windows-based pipeline agents.
- Free to use.
How It Works
The task requires three inputs:
| Input |
Description |
| Source File |
JSON file containing the source key-value pairs |
| Destination Path |
Root folder containing the destination JSON files |
| File Filter |
Name of the JSON files that should be processed |
The task reads values from the Source File, searches the Destination Path recursively, and replaces matching placeholders in the destination JSON files.
1. Source File
The Source File contains the key-value pairs that will be used for replacement.
Requirements
- The source file must be a
.json file.
- The source JSON must contain only plain key-value pairs.
- The source JSON must not contain nested JSON objects.
- Provide the full path to the source JSON file.
Example
{
"ApiUrl": "https://uat-api.example.com",
"Timeout": "60",
"DatabaseString": "UATDB",
"LogLevel": "Warning"
}
In this example:
ApiUrl contains the API URL.
Timeout contains the API timeout.
DatabaseString contains the database value.
LogLevel contains the logging level.
2. Destination Path
The Destination Path is the root folder where the task searches for destination JSON files.
The task searches the specified folder recursively, including all subfolders.
Example
C:\Applications\MyApplication
3. File Filter
The File Filter specifies the name of the JSON file that should be processed.
Example
appsettings.json
Only JSON files matching the specified file name are processed.
Example Directory Structure
Consider the following application directory:
MyApplication
│
├── appsettings.json
│
├── Service1
│ └── appsettings.json
│
├── Service2
│ ├── appsettings.json
│ └── Config
│ └── appsettings.json
│
└── Other
└── settings.json
If the File Filter is:
appsettings.json
the task processes:
MyApplication\appsettings.json
MyApplication\Service1\appsettings.json
MyApplication\Service2\appsettings.json
MyApplication\Service2\Config\appsettings.json
The following file is ignored:
MyApplication\Other\settings.json
because its file name is settings.json and does not match the configured filter.
Target JSON
The destination JSON file can contain nested JSON objects.
The destination file can also contain placeholders using the following format:
$(KeyName)
Example Target JSON
{
// Database configuration
"Database": {
"ConnectionString": "$(DatabaseString)"
},
// API configuration
"Api": {
"Url": "$(ApiUrl)",
"Timeout": $(Timeout)
},
// Logging configuration
"Logging": {
"LogLevel": "$(LogLevel)"
}
}
The task searches the destination JSON for placeholders and matches them with keys from the source JSON.
Replacement Process
Given the following source JSON:
{
"ApiUrl": "https://uat-api.example.com",
"Timeout": "60",
"DatabaseString": "UATDB",
"LogLevel": "Warning"
}
and the target JSON:
{
// Database configuration
"Database": {
"ConnectionString": "$(DatabaseString)"
},
// API configuration
"Api": {
"Url": "$(ApiUrl)",
"Timeout": $(Timeout)
},
// Logging configuration
"Logging": {
"LogLevel": "$(LogLevel)"
}
}
the task replaces each matching placeholder with the corresponding value from the source JSON.
Output
After replacement, the target JSON becomes:
{
// Database configuration
"Database": {
"ConnectionString": "UATDB"
},
// API configuration
"Api": {
"Url": "https://uat-api.example.com",
"Timeout": 60
},
// Logging configuration
"Logging": {
"LogLevel": "Warning"
}
}
Key Matching
The task matches placeholders in the destination JSON with keys in the source JSON.
For example:
| Source Key |
Destination Placeholder |
Result |
ApiUrl |
$(ApiUrl) |
https://uat-api.example.com |
Timeout |
$(Timeout) |
60 |
DatabaseString |
$(DatabaseString) |
UATDB |
LogLevel |
$(LogLevel) |
Warning |
The key name must match the placeholder name.
For example:
Source Key:
ApiUrl
matches:
$(ApiUrl)
but does not match:
$(Api.Url)
Azure DevOps Pipeline Usage
The task can be used in both:
- Azure DevOps Build Pipelines
- Azure DevOps Release Pipelines
A typical configuration can be:
Source File:
C:\Config\UAT.json
Destination Path:
C:\Applications\MyApplication
File Filter:
appsettings.json
The task then:
- Reads the source JSON file.
- Reads the source key-value pairs.
- Searches the destination folder recursively.
- Finds files matching the File Filter.
- Searches the target JSON files for matching placeholders.
- Replaces the placeholders with values from the source JSON.
- Leaves unmatched values unchanged.
Environment-Specific Configuration
This task is useful when the same application package is deployed to multiple environments.
For example:
Development
QA
UAT
Production
Each environment can have its own source JSON file:
DEV.json
QA.json
UAT.json
PROD.json
The same application package can then be deployed to different environments while applying environment-specific configuration values during the pipeline execution.
Important Notes
- The Source File must contain plain key-value pairs only.
- Nested JSON objects are not supported in the Source File.
- Nested JSON objects are supported in the Target JSON.
- The task searches the Destination Path recursively.
- Only files matching the configured File Filter are processed.
- Placeholders should use the
$(KeyName) format.
- The placeholder name should correspond to a key in the source JSON.
- Values can be replaced inside nested destination objects.
- JSON files with different names from the configured File Filter are ignored.
- The task is designed for Windows-based Azure DevOps pipeline agents.
Summary
Replace JSON Config provides a simple way to automate JSON configuration updates during Azure DevOps deployments.
Instead of manually modifying configuration files for each environment, maintain environment-specific values in a simple source JSON file and allow the task to automatically replace matching placeholders in your application's destination JSON files.