Read and set the variable on the Windows command line from the .json file

I have this little config.json :

{
    "version":"0.2.2",
    "test":true
}

I would like to read this file and set the variables on the command line of the window, corresponding to the keys and values ​​from the file json, maybe something like this pseudocode:

for /f %%A in (config.json) do (

    SET key = value
)

echo %VERSION%
echo %TEST%

How can I do it?

+4
source share
1 answer

This solves the question:

@echo off
setlocal EnableDelayedExpansion
set c=0
for /f "tokens=2 delims=:, " %%a in (' find ":" ^< "config.json" ') do (
   set /a c+=1
   set val[!c!]=%%~a
)
for /L %%b in (1,1,!c!) do echo !val[%%b]!
pause

This version will output version = 0.2.2and the test = true
only change in line 6

@echo off
setlocal EnableDelayedExpansion
set c=0
for /f "tokens=1,2 delims=:, " %%a in (' find ":" ^< "config.json" ') do (
   set /a c+=1
   set val[!c!]=%%~a = %%~b
)
for /L %%b in (1,1,!c!) do echo !val[%%b]!
pause

This solves the following format request: variable-name = value

@echo off
for /f "tokens=1,2 delims=:, " %%a in (' find ":" ^< "config.json" ') do (
   set "%%~a=%%~b"
)
set
pause

A request from February 2016 can be resolved given that the file looks like this:

 {
 location : "C:\\Test Folder"
 }

Package script below

@echo off
for /f "tokens=1,2,*" %%a in (' find ":" ^< "config.json" ') do echo "%%~c"
pause
+6

All Articles