Reputation: 75
I have the Json file called Services.json with following content:
{
"name":"Services",
"version":"1.2.0",
"description":"Customer Services"
}
I want to read this file and while reading if it finds "version" key then save respective value(1.2.0) into a variable using command line script
I tried something like this but it didn't work.
@Echo off
for /f "tokens=1,2 delims=:{} " %%A in (Services.json) do (
If "%%~A"=="version" (
set version = "%%~b"
)
)
pause
Upvotes: 1
Views: 1358
Reputation: 67216
This pure Batch solution get the values of all variables:
@echo off
setlocal EnableDelayedExpansion
rem Get variables
set "var="
for /F "tokens=1* delims=:{} " %%a in (Services.json) do (
for %%c in (%%a %%b) do (
if not defined var (set "var=%%~c") else set "!var!=%%~c" & set "var="
)
)
rem Show values
echo name=%name%
echo version=%version%
echo description=%description%
Output:
name=Services
version=1.2.0
description=Customer Services
New method assuming that "the keyvalue pair is in a single line":
@echo off
setlocal
rem Get variables
set /P "json=" < Services.json
set "json=%json:{=%"
set "json=%json:}=%"
set "json=%json:":"==%"
set %json:,= & set %
rem Show values
echo name=%name%
echo version=%version%
echo description=%description%
Upvotes: 1
Reputation: 80023
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\Q74814659.txt"
FOR /f "usebackqtokens=1,2delims=:, " %%b IN ("%filename1%") DO IF %%b=="version" SET "version=%%~c"
ECHO version=%version%
GOTO :EOF
It's been asked before, but it's easier for me to re-write it again that look it up.
--- Revision since the JSON file is actually 1 line
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\Q74814659_2.txt"
SET /p json=<"%filename1%"
SET "json=%json:{=%"
SET "json=%json:}=%"
FOR %%e IN (%json%) DO (
FOR /f "tokens=1,2delims=:" %%b in ("%%e") do FOR %%y IN (version description name) DO IF /i "%%y"==%%b SET "%%y=%%~c"
)
ECHO version=%version%
ECHO name=%name%
ECHO description=%description%
GOTO :EOF
rem Always verify against a test directory before applying to real data.
Read the data to json
, remove all braces, process json
as a comma-separated list of elements "name":"value"
Check whether the name
is on the list; if so, assign the value
.
Upvotes: 1
Reputation: 18827
You can parse your JSON file using PowerShell and set it as a variable in your batch file with for /f..do
loop command like this example :
@echo off
Title Get Version from Services.json using PowerShell with a batch file
Set PSCMD=Powershell -C "$(GC Services.json | ConvertFrom-Json).version"
@for /f %%a in ('%PSCMD%') do set "Ver=%%a"
echo Version=%Ver%
pause
Upvotes: 1