How to Parse JSON Files in Batch Script (via PowerShell)
JSON (JavaScript Object Notation) has become the de-facto standard for data interchange, used everywhere from web APIs to configuration files. As a scripter, you will frequently encounter the need to read a value from a JSON file. However, the Windows Batch interpreter (cmd.exe) is a simple shell from an era before JSON existed; it has no native, built-in ability to understand or parse the JSON format.
This guide will explain why a "pure-batch" approach is not feasible and will teach you the definitive, modern, and only recommended method: calling a PowerShell one-liner from your batch script. You will learn how to use PowerShell's powerful ConvertFrom-Json cmdlet to easily read any value from a JSON file, including from nested objects and arrays.
The Challenge: Why Pure Batch Can't Parse JSON
JSON's structure, with its nested curly braces ({}), square brackets ([]), quotes, and commas, is far too complex for batch's simple text-parsing tools like FOR /F. Attempting to find a specific value with findstr and string manipulation would be incredibly difficult, brittle, and would fail the moment the formatting of the JSON changed (e.g., if it was "minified" onto a single line). To parse JSON correctly, you need a true JSON parser.
The Core Method (Recommended): Using PowerShell's ConvertFrom-Json
The correct and professional way to handle this is to call PowerShell, which has a powerful, built-in JSON parser.
Syntax
powershell -Command "(Get-Content -Raw 'file.json' | ConvertFrom-Json).PropertyName"
Get-Content -Raw 'file.json': This reads the entire JSON file into a single string. The-Rawswitch is important as it prevents the file from being split into lines, which can break multi-line JSON values.| ConvertFrom-Json: This is the core cmdlet. It takes the JSON string and converts it into a structured PowerShell object..PropertyName: Once converted, you can access the values using simple dot-notation.
Basic Example: Reading a Simple JSON File
Let's read a simple configuration file, like the following one:
{
"AppName": "My Cool App",
"Version": "1.2.3",
"Author": "Admin"
}
This script uses PowerShell to read the Version from the file.
@ECHO OFF
ECHO --- Reading Version from JSON ---
powershell -Command "(Get-Content -Raw 'config.json' | ConvertFrom-Json).Version"
Output:
--- Reading Version from JSON ---
1.2.3
How to Capture a Specific Value in a Variable
This is the standard pattern for getting a single JSON value into a batch variable.
To use the value in your script, you capture the output of the PowerShell command with a FOR /F loop.
@ECHO OFF
SET "AppVersion="
FOR /F "delims=" %%V IN (
'powershell -Command "(Get-Content -Raw 'config.json' | ConvertFrom-Json).Version"'
) DO (
SET "AppVersion=%%V"
)
ECHO The application version is: %AppVersion%
Working with Nested Objects and Arrays
This is where the power of PowerShell really shines. You can easily drill down into complex structures using dot-notation.
{
"Server": {
"IPAddress": "192.168.1.100",
"Port": 8080
},
"EnabledFeatures": [
"Login",
"Reporting",
"Audit"
]
}
And the following script is used to get the IP address from a nested object:
@ECHO OFF
FOR /F "delims=" %%I IN (
'powershell -Command "(Get-Content -Raw 'complex_config.json' | ConvertFrom-Json).Server.IPAddress"'
) DO (
SET "ServerIP=%%I"
)
ECHO Server IP is: %ServerIP%
While, tne following script is used to get the second feature (i.e. the array) of the json.
Remember that arrays are zero-indexed, so the second item is at index 1.
@ECHO OFF
FOR /F "delims=" %%F IN (
'powershell -Command "(Get-Content -Raw 'complex_config.json' | ConvertFrom-Json).EnabledFeatures[1]"'
) DO (
SET "Feature=%%F"
)
ECHO The second feature is: %Feature%