如何清除json中的特定字段,在子json中使用批处理文件具有类似的字段?



当我在config.json中运行批处理脚本时,我希望密码字段仅为user1设置为空白。

config.json包含以下数据

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

你知道怎么做吗?

实际上我是新的批处理脚本,但我能够改变所有的密码字段

@ECHO Off
SETLOCAL
rem The following settings for the directories and filenames 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 "destdir=u:your results"
SET "filename1=%sourcedir%q74457179.txt"
SET "outfile=%destdir%outfile.txt"
CALL :zapflags 
SET "indent=    "
(
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
SET "repro=y"
FOR %%y IN (%%e) DO IF DEFINED fpass (
SET "repro="
CALL :zapflags 
ECHO %indent%"password": ""
) ELSE (
IF DEFINED fuser1 IF %%y=="password": (SET "fpass=y"&SET "fuser=") ELSE (CALL :zapflags) 
IF DEFINED fuser IF %%~y==user1 (SET "fuser1=y") ELSE (CALL :zapflags) 
IF %%y=="user": SET "fuser=y"
)
IF DEFINED repro ECHO %%e
)
)>"%outfile%"
TYPE "%outfile%"
GOTO :eof
:zapflags 
FOR %%z IN (fuser fuser1 fpass) DO SET "%%z=")
GOTO :EOF

在应用于实际数据之前,始终针对测试目录进行验证。

请注意,如果文件名不包含分隔符,如空格,则usebackq%filename1%周围的引号都可以省略。

将文件的每行读取到%%e,注意标志fuser,fuser1fpass都被清除(未定义)

我们要么简单地按原样复制该行,要么替换它。将标志repro设置为y(任意值),表示reproduce this line

分析每行中的子字符串。子字符串之间用逗号或空格分隔。

如果子串"user":出现,设置标志fuser
如果子串user1出现,设置标志fuser1,否则清除f*标志
如果子串"password":出现,设置标志fpass并清除标志fuser,否则清除f*标志
下面的密码应该被跳过并替换为空字符串,因此清除repro标志以防止原始行被复制,清除f*标志并返回替换行

最新更新