Skip to main content

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 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 -Raw switch 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:

config.json
{
"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.

complex_config.json
{
"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%

Common Pitfalls and How to Solve Them

Problem: The JSON File is Invalid

If your config.json file has a syntax error (like a missing comma or quote), the ConvertFrom-Json cmdlet will fail and throw an error.

Solution: This is expected behavior. The JSON file itself must be valid. Ensure the source of your JSON is producing well-formed content.

Problem: The Property Does Not Exist or is Null

If you try to access a property that doesn't exist (e.g., .Author in complex_config.json), the PowerShell command will produce no output.

Solution: In your batch script, the FOR /F loop will not run, and the variable you are trying to set will remain undefined. You can check for this with IF DEFINED.

IF DEFINED MyVar (
ECHO The value is: %MyVar%
) ELSE (
ECHO The property was not found in the JSON file.
)

Practical Example: Reading an Application Configuration File

This script reads a server address and port from a JSON config file and then uses those values to launch another command (like ping).

@ECHO OFF
SETLOCAL
SET "CONFIG_FILE=config.json"

ECHO --- Application Launcher ---
IF NOT EXIST "%CONFIG_FILE%" (ECHO [ERROR] Config file not found! & GOTO :End)

ECHO Reading configuration from "%CONFIG_FILE%"...

REM --- Get the Server IP ---
FOR /F "delims=" %%I IN ('powershell -Command "(Get-Content -Raw '%CONFIG_FILE%' | ConvertFrom-Json).Server.IPAddress"') DO (
SET "ServerIP=%%I"
)

REM --- Get the Server Port ---
FOR /F "delims=" %%P IN ('powershell -Command "(Get-Content -Raw '%CONFIG_FILE%' | ConvertFrom-Json).Server.Port"') DO (
SET "ServerPort=%%P"
)

IF NOT DEFINED ServerIP (ECHO [ERROR] Could not read ServerIP from config. & GOTO :End)

ECHO.
ECHO Server IP: %ServerIP%
ECHO Server Port: %ServerPort%
ECHO.
ECHO Pinging the server to check connectivity...
PING -n 2 "%ServerIP%"

:End
ENDLOCAL

Conclusion

While batch script has no native ability to handle JSON, it can easily delegate the task to PowerShell, which is a first-class JSON parser built into all modern versions of Windows.

Key takeaways:

  • Do not attempt to parse JSON with pure batch commands. It is not reliable.
  • The PowerShell ConvertFrom-Json cmdlet is the only correct and recommended method.
  • Use the syntax: powershell -Command "(Get-Content -Raw 'file.json' | ConvertFrom-Json).Property"
  • Use dot-notation (.Property.NestedProperty) and array indexers ([0]) to access any value within the JSON structure.
  • Capture the result in a FOR /F loop to use the value in your batch script.