1

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
3
  • 1
    Your question has been asked, and answered before, even using a batch file. I would however strongly advise that you instead seek a solution using PowerShell. Please use the search facility at the top of the page, to locate, then adapt an answer for your purposes. Commented Dec 15, 2022 at 16:29
  • Yes, I tried those links but those doesn't seem to be same. Here in this case I want to save version key value into a variable. Commented Dec 15, 2022 at 16:46
  • It's been done before, show us the code you've failed with, together with a link to the results you used to create it, and we'll try to assist. Commented Dec 15, 2022 at 16:51

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to do it using batch script.
@SandeepSangshetty Did you tested it or not yet ? It's just one command that can be used in your batch file, and store your variable as you want.
1
@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.

6 Comments

The code works as posted, so why did you change the value of delims? What did you change it to?
I changed the delims and could read the value of "name" but not "version" FOR /f "tokens=1,2 delims=:,{}" %%b IN (D:\test\versionInfo.json) DO IF "%%~b"=="version" SET "version=%%~c" ECHO version=%version% output: version= It worked for "name". FOR /f "tokens=1,2 delims=:,{}" %%b IN (D:\test\versionInfo.json) DO IF "%%~b"=="name" SET "name=%%~c" ECHO name=%name% ouput: name=activate-enterprise-consumer-services Pls suggest any improvement needed to read version
I noticed that it reads this first key value in the json text. Like if I remove "name" key/value then logic is working fine. { "version":"1.2.0", "description":"Customer Services" } Any inputs here?
Please cut-and-paste the original code. The braces ({}) are irrelevant and you have omitted the space from the delims option. It certainly works as-is for version and name. Some extra gymnastics would be required to extract description as the value assigned to description contains a space, which is one of the required delimiters.
I double checked from my end, your logic worked fine when each keyvalue pair is in a new line. The same logic didn't work, when the keyvalue pair is in a single line. May be its my bad that I posted the Json content which has new lines where as my actual content doesn't have line breaks.
|
1

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%

1 Comment

See my New method... :(
-1

I suggest to use a structure-aware tool like jq to get content from a JSON file.

$version = (jq -r '.version' Services.json)

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.