windows - 如何使用批处理文件清除 json 中的字段?

标签 windows batch-file

我希望在我的 config.json 中运行脚本时将密码字段设置为空白,它包含以下数据

{
  "data": {
    "user": "user1",
    "password": "Password" 
  },
  "data2":{    
    "user": "user2",
    "password": "Password2"
  }
}

知道怎么做吗?

最佳答案

您可以使用 RegEx 尝试使用此混合代码 Batch+Vbscript:

@echo off
Mode 52,3 & color 0A
Title Clear a field in json using batch file
Set "Tmpvbs=%temp%\Tmpvbs.vbs"
Set "InputFile=config.json"
Set "OutPutFile=result.txt"
Call :ReplaceData "%InputFile%" "%OutPutFile%"
echo(
Echo  All passwords fields in "%InputFile%" are cleared
TimeOut /T 3 /nobreak>nul & exit
::****************************************************
:ReplaceData <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Set myRegExp = New RegExp
echo myRegExp.Global = True
echo myRegExp.Pattern = "(""password"":)(\W.*)"
echo ResultString = myRegExp.Replace(Data, """password"": """""^)
echo WScript.echo ResultString
)>"%Tmpvbs%"
cscript //nologo "%Tmpvbs%" < "%~1" > "%~2"
Move /y "%~2" "%~1">nul
If Exist "%Tmpvbs%" Del "%Tmpvbs%"
exit /b
::****************************************************

因此,您可以获得这样的 config.json:

{
  "data": {
    "user": "user1",
    "password": ""
  },
  "data2":{    
    "user": "user2",
    "password": ""
  }
}

关于windows - 如何使用批处理文件清除 json 中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49044817/

相关文章:

C++使用系统获取PID

windows - 将具有特殊字符的参数从一个批处理文件发送到另一个批处理文件

batch-file - 如何将值从一个批处理文件返回到调用者批处理文件

windows - 在Windows命令行中获取卷名的驱动器号

windows - 在命令提示符下复制文件夹

html - Windows 如何将 HTML 文件链接到文件夹?

linux - 在cmd文件中传递变量并在linux中执行

windows - 从函数内部退出批处理脚本

batch-file - 批处理 : get last folder name from path from txt

用于打开和关闭代理的 Windows 桌面小部件