Here's a batch way to transform the string
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "postman={ "name": "name-A", "id": "name-id", "providers": ["prov-a, prov-b, prov-c"]}"
FOR /f "tokens=1,2,*delims=[]" %%u IN ("%postman%") DO (
SET "postman=%%~v"
SET "postman=%%u["!postman: =","!"]%%w"
)
ECHO postman=%postman%
GOTO :EOF
The for
separates the string of interest between the [
and ]
. assigning the part before the [
to %%u
, that after ]
to %%w
and the string to %%v
. In order to manipulate %%v
, it needs to be assigned to a user variable. %%~v
removes the enclosing quotes Then re-string %%u
and %%w
with the brackets and replace the spaces in (%%v
) with ","
and quote the result.
This worked for me in W10. What I find intriguing is that the commas logically should appear in %%v
but do not.
Revision -------------
In the light of the newest revision of the "actual" code produced,
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q73031428.txt"
(
FOR /f "usebackqtokens=1,2,*delims=[]" %%u IN ("%filename1%") DO IF "%%~v"=="" (
ECHO %%u
) ELSE (
SET "postman=%%~v"
SET "postman=!postman:,=!"
ECHO %%u["!postman: =","!"]%%w
)
)>replacementfilename
GOTO :EOF
Note that if the filename does not contain separators like spaces, then both usebackq
and the quotes around %filename1%
can be omitted.
I've assumed the code is available in file q73031428.txt
Really, it's the same code but revised since the newest layout available contains newlines where the original didn't.
Intriguingly also, %%v
this time includes commas, hence they're removed in the new version.