将 json 配置值获取到 windows 脚本变量中
Get json config value into windows script variable
我想解析 config.json 文件并将一些特定值放入变量中。
以下命令正在运行,它打印来自 json 的值:
type config.json | "jq-win64.exe" .path.editor
config.json内容:
{ "path" : { "editor" : "...value..." } }
jq 是 json (https://stedolan.github.io/jq/)
的库
问题是如何将此值放入某个变量?
我试过了,但没有用:
set editorPath = type config.json | "jq-win64.exe" -r .path.editor
结果是%editorPath%
为空
可能与 jq 库 (https://stedolan.github.io/jq/) 有关,但我在 windows shell 方面很菜鸟,所以也许有人可以帮助解决这个问题。
谢谢
一般来说,当您想要将命令的输出捕获到变量时,请使用for /F
循环。 for /f "delims=" %%I in ('command') do set "variable=%%I"
或类似的。有关详细信息,请参阅 cmd 控制台中的 help for
。
可以使 JScript 解释 JSON 类似于 JavaScript 的方式。有security concerns;但是如果您可以合理地确定没有人可能将恶意代码插入 JSON,您可以在不需要第 3 方工具的情况下解析它。
这个示例脚本结合了上面提到的两个想法——用 cscript.exe
调用 JScript 代码块,并用 for /f
循环捕获它的输出。
config.json:
{ "path" : { "editor" : "...value..." } }
parseJSON.bat:
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "JSONfile=config.json"
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%JSONfile%"') do set "%%~I"
rem // Delayed expansion prevents path names with symbols (& or %) from choking the script
setlocal enabledelayedexpansion
echo editor: !editor!
endlocal
goto :EOF
@end // end batch / begin JScript chimera
var fso = WSH.CreateObject('scripting.filesystemobject'),
JSONfile = fso.OpenTextFile(WSH.Arguments(0), 1);
eval('obj = ' + JSONfile.ReadAll());
JSONfile.Close();
function walk(tree) {
for (var i in tree) {
if (typeof tree[i] === 'object') walk(tree[i]);
else WSH.Echo(i + '=' + tree[i]);
}
}
walk(obj);
输出:
editor: ...value...
以下是使用 jq 的两种方法的示例:
@echo off
setlocal
for /f "delims=" %%I in ('jq -n -r "\"123\""') do set A=%%I
echo A is %A%
jq -n -r "@sh \"set B=123\"" > setvars.bat
call .\setvars.bat
echo B is %B%
第一个例子,批处理文件决定了变量名;在第二个示例中,jq 程序确定变量名称。
顺便说一下,对于非 Windows 用户,在 jq Cookbook.
中有一个关于此的 "recipe"
这对 Xidel 来说非常容易。
点符号:
xidel -s config.json -e "($json).path.editor"
XPath 表示法:
xidel -s config.json -e "$json/path/editor"
or even shorter
xidel -s config.json -e "$json//editor"
将值导出到 "editorPath" 变量:
FOR /F "delims=" %A IN ('xidel -s config.json -e "editorPath:=$json//editor" --output-format^=cmd') DO %A
我想解析 config.json 文件并将一些特定值放入变量中。
以下命令正在运行,它打印来自 json 的值:
type config.json | "jq-win64.exe" .path.editor
config.json内容:
{ "path" : { "editor" : "...value..." } }
jq 是 json (https://stedolan.github.io/jq/)
的库
问题是如何将此值放入某个变量?
我试过了,但没有用:
set editorPath = type config.json | "jq-win64.exe" -r .path.editor
结果是%editorPath%
为空
可能与 jq 库 (https://stedolan.github.io/jq/) 有关,但我在 windows shell 方面很菜鸟,所以也许有人可以帮助解决这个问题。
谢谢
一般来说,当您想要将命令的输出捕获到变量时,请使用for /F
循环。 for /f "delims=" %%I in ('command') do set "variable=%%I"
或类似的。有关详细信息,请参阅 cmd 控制台中的 help for
。
可以使 JScript 解释 JSON 类似于 JavaScript 的方式。有security concerns;但是如果您可以合理地确定没有人可能将恶意代码插入 JSON,您可以在不需要第 3 方工具的情况下解析它。
这个示例脚本结合了上面提到的两个想法——用 cscript.exe
调用 JScript 代码块,并用 for /f
循环捕获它的输出。
config.json:
{ "path" : { "editor" : "...value..." } }
parseJSON.bat:
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "JSONfile=config.json"
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%JSONfile%"') do set "%%~I"
rem // Delayed expansion prevents path names with symbols (& or %) from choking the script
setlocal enabledelayedexpansion
echo editor: !editor!
endlocal
goto :EOF
@end // end batch / begin JScript chimera
var fso = WSH.CreateObject('scripting.filesystemobject'),
JSONfile = fso.OpenTextFile(WSH.Arguments(0), 1);
eval('obj = ' + JSONfile.ReadAll());
JSONfile.Close();
function walk(tree) {
for (var i in tree) {
if (typeof tree[i] === 'object') walk(tree[i]);
else WSH.Echo(i + '=' + tree[i]);
}
}
walk(obj);
输出:
editor: ...value...
以下是使用 jq 的两种方法的示例:
@echo off
setlocal
for /f "delims=" %%I in ('jq -n -r "\"123\""') do set A=%%I
echo A is %A%
jq -n -r "@sh \"set B=123\"" > setvars.bat
call .\setvars.bat
echo B is %B%
第一个例子,批处理文件决定了变量名;在第二个示例中,jq 程序确定变量名称。
顺便说一下,对于非 Windows 用户,在 jq Cookbook.
中有一个关于此的 "recipe"这对 Xidel 来说非常容易。
点符号:
xidel -s config.json -e "($json).path.editor"
XPath 表示法:
xidel -s config.json -e "$json/path/editor"
or even shorter
xidel -s config.json -e "$json//editor"
将值导出到 "editorPath" 变量:
FOR /F "delims=" %A IN ('xidel -s config.json -e "editorPath:=$json//editor" --output-format^=cmd') DO %A