Extracting a Specific JSON Section in Azure RunCommand
Reduce Console Noise: Extract Only What You Need
When using Azure RunCommand on a virtual machine, the output console is limited. If you print an entire configuration file, large parts may be cut off. This makes troubleshooting difficult, especially when you only need a small section such as the environment node.
Instead of displaying the full file, you can extract just the required part. This keeps the output small, clear, and readable. It also helps you work safely with large configuration files in production systems.
In this guide, you will see two practical approaches:
- A structured JSON method (best when the file is valid JSON)
- A multiline Regex method (useful when the file is not valid JSON)
Both approaches are written for PowerShell and work well inside Azure RunCommand. The examples use small sample files to stay focused on the solution itself.
Each section explains one method clearly so you can choose the right technique for your scenario.
Safe and Clean Extraction from a Valid JSON File
If your configuration file is valid JSON, parsing it is the safest and most reliable method.
Small example file:
{
"application": "SampleApp",
"environment": {
"name": "Production",
"debug": false
}
}
PowerShell solution:
$content = Get-Content "appsettings.json" -Raw
$json = $content | ConvertFrom-Json
$json.environment
This command:
- Reads the whole file correctly
- Converts it into a PowerShell object
- Extracts only the
environmentnode - Prints only the required section to the console
If you want formatted JSON output:
$json.environment | ConvertTo-Json -Depth 3
Why this method is recommended:
- It understands JSON structure
- It avoids incorrect matches
- It works reliably with nested properties
- It keeps Azure RunCommand output short
Always use this approach when the file is valid JSON. It is clean, simple, and suitable for production environments.
When JSON Parsing Is Not Possible
Sometimes configuration files are not valid JSON. They may contain comments or formatting issues. In such cases, ConvertFrom-Json will fail. When that happens, you can extract the environment section using a multiline regular expression.
Small example file:
{
"application": "SampleApp",
"environment": {
"name": "Production",
"debug": false
},
// comment
"logging": "Information"
}
PowerShell Regex solution:
$content = Get-Content "appsettings.json" -Raw
if ($content -match '(?s)"environment"\s*:\s*\{.*?\}') {
$matches[0]
}
Explanation:
(?s)allows the dot to match across multiple lines.*?ensures non-greedy matching- The pattern extracts the full
environmentobject
This method works well when:
- The structure is simple
- You need one known section
- Full JSON parsing is not possible
Important: Regex does not fully understand nested JSON structures. Use it only when parsing cannot be used.
For Azure RunCommand scenarios with limited console output, this method provides a focused and practical solution.
Comments