Passing arguments with spaces in it to a subroutine
Posted: 13 Jul 2019 04:24
Hello,
I'm working with JSONs and I'm having trouble correctly passing these JSONs as arguments with spaces in it to a subroutine.
The following simple JSON, {"a": 1, "b": 2}, is working fine:
Output:
So despite there being spaces between the attribute and value, this is no problem it seems.
The following simple JSON with spaces in the attribute's values, {"a": "a b c", "b": "x y z"}, is NOT working correctly:
Output:
Where as the expected/desired output was:
I have also looked at ECHO %* which results in:
It first of all quotes the entire string (sadly %~* doesn't work) and then strangely enough escapes only the first escape-character ( ^ ) and removes the second, but leaves the rest intact...
Does anyone know how I can have my script output the expected/desired output as shown above?
I'm working with JSONs and I'm having trouble correctly passing these JSONs as arguments with spaces in it to a subroutine.
The following simple JSON, {"a": 1, "b": 2}, is working fine:
Code: Select all
SET json={^^^"a^^^": 1^^^, ^^^"b^^^": 2}
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :Info "!json!"
ENDLOCAL
EXIT /B 0
:Info
ECHO %~1
EXIT /B
Code: Select all
{^"a^": 1^, ^"b^": 2}
The following simple JSON with spaces in the attribute's values, {"a": "a b c", "b": "x y z"}, is NOT working correctly:
Code: Select all
SET json={^^^"a^^^": ^^^"a b c^^^"^^^, ^^^"b^^^": ^^^"x y z^^^"}
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :Info "!json!"
ENDLOCAL
EXIT /B 0
:Info
ECHO %~1
EXIT /B
Code: Select all
{^"a^": ^"a
Code: Select all
{^"a^": ^"a b c^"^, ^"b^": ^"x y z^"}
Code: Select all
"{^^"a": ^"a b c^"^, ^"b^": ^"x y z^"}"
Does anyone know how I can have my script output the expected/desired output as shown above?