0

I have a JSON file, named "Config.json", that looks like this:

{ "RunEnvironment": "DEV"}

In a batch file under the same directory, I want to read the value of the "RunEnvironment" element. My batch script would look like:

if [jsonElement] == 'DEV' ( :: do something )

Can anyone show me how to do this?

4
  • Better use a script language with tools for handling json files. For a start in cmd line for /f "tokens=1,2 delims=:{} " %A in (file.json) do @If "%~B"=="Dev" @Echo %~A = %~B in a batch file double the percent signs. Commented Jun 12, 2019 at 10:45
  • 1
    If you still wish to use a script language with no specific json handling tools, then it's important to note that the .json file may carry several [jsonElement]'s with values on the same line and those lines may or may not contain opening or closing braces and may have trailing commas. This would mean that you would need to isolate lines containing the string first, then parse that line to determine your specific element and value within it. The answer I provided uses FindStr to isolate the lines, so that may help if my answer was not exactly what you needed. Commented Jun 12, 2019 at 13:00
  • Sometimes you just have to say stop, you are using the wrong tool. Batch files are just not the best choice for this, their stringhandling is subpar and writing a robust parser is just not possible. Your only hope is to get something that can read your value assuming there are no strange characters in the file but it is never going to be a real JSON parser. Commented Jun 12, 2019 at 13:03
  • @Anders He doesn't want a real JSON parser, he just needs a quick hack to get some data. I need this too to automate a simple repeating task for a certain test. This question helped me a lot. Commented May 26, 2021 at 6:56

4 Answers 4

4

In PowerShell for example, you could do:

> If((Get-Content '.\Config.json'|ConvertFrom-Json).RunEnvironment -eq 'DEV'){"is DEV:Whatever"}
is DEV:Whatever

To be on topic on cmd line

> for /f "tokens=1,2 delims=:{} " %A in (Config.json) do @If "%~B"=="Dev" @Echo (%~A = %~B)
(RunEnvironment=DEV)

In a batch file

@Echo off
for /f "tokens=1,2 delims=:{} " %%A in (Config.json) do (
    If "%%~B"=="Dev" Echo (%%~A=%%~B^)
)

The Echo (%%~A=%%~B^) could be replaced with whatever you plan to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a note to add that the ConvertFrom-JSON cmdlet was introduced in powershell-v3.0, so may not be available in earlier operating systems like windows-7, without specifcally installing an updated powershell version.
Having multiple values in my json, I had to update delims to delims=:{},
1

If all you need to do is perform a specific command only if the value of RunEnvironment is DEV then this should be all you require, (from a or the ):

"%__APPDIR__%FindStr.exe" /IRC:"\"RunEnvironment\":\ \ *\"DEV\"" "Config.json">NUL 2>&1&&Echo Your command here

You'd simply replace Echo Your command here with your intended command.

Comments

0

Yo can do in this way

@echo off 
set string={ "RunEnvironment": "DEV" }

for /f "tokens=3,5" %%a in ('echo %string%') do set d=%%~a 
if "%d%" == "DEV" echo %d%   
pause & goto :EOF

1 Comment

Would it be possible to read directly from the JSON file rather than the "set string = ..." line ?
0

In a batch file:

@ECHO off

SET "FilenameForJsonFile=Config.json"
SET "FilenameForRunEnvironment=RunEnvironment.txt"

powershell -Command "Select-String -Pattern 'RunEnvironment\"\: \".*?\"' .\%FilenameForJsonFile% ^| ForEach-Object { $_.Matches.Value.substring(18,$_.Matches.Value.Length-19) }>%FilenameForRunEnvironment%

FOR /f "delims=" %%x IN (%FilenameForRunEnvironment%) DO SET JsonElement=%%x
       
IF %JsonElement%==DEV (ECHO development environment) ELSE (ECHO abc123 environment)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.