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

问题描述 投票:2回答:1

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

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

知道怎么做吗?

windows batch-file
1个回答
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": ""
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.