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

时间:2018-03-01 07:40:42

标签: windows batch-file

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

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

知道怎么做吗?

1 个答案:

答案 0 :(得分:1)

您可以使用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": ""
  }
}