iTranslated by AI
PowerShell: Double Quote Escaping with `""` Behavior Changes Based on Whitespace
Environment
- Windows 10
- PowerShell 5.1.19041.1151
- Python 3.8
Goal
I want to pass a JSON string as a command-line argument.
Since JSON strings include double quotes, some handling is required.
For example, in bash, you can correctly pass double quotes within a JSON string as a command-line argument by enclosing the JSON string in single quotes.
As a sample, I will execute a Python script that outputs the first argument.
import sys
print(sys.argv[1])
$ python print_arg1.py '{"x": 1}'
{"x": 1}
I want to do the same thing in Windows PowerShell.
Problem
I skimmed the following article and interpreted it as "to escape double quotes in a JSON string, you can write double quotes twice in a row" (I suspect this might not be entirely correct...).
I executed the python command with double quotes written consecutively in the JSON string.
# Not escaped
PS > python .\print_arg1.py '{"x": 1}'
{x: 1}
# Escaped
PS > python .\print_arg1.py '{""x"": 1}'
{"x": 1}
# Not escaped?!
PS > python .\print_arg1.py '{""x"":1}'
{x:1}
It was escaped when there was a space in the JSON string, but not escaped when there was no space.
Cause?
I couldn't identify the cause, but I found a sentence that seems relevant in the answer to the following question.
Powershell will auto-quote (enclose in <">) a single argument string, if it contains spaces and the spaces don't mix with an uneven number of (unsescaped) double quotes.
Solution
I solved it by escaping double quotes with a backslash.
PS > python .\print_arg1.py '{\"x\":1}'
{"x":1}
Reference: https://stackoverflow.com/questions/53490414/how-to-pass-json-string-data-to-powershell
Supplement
In the case of Command Prompt
# ==== Enclosing JSON string in single quotes ====
# NG
> python .\print_arg1.py '{"x": 1}'
'{x:
# NG
> python .\print_arg1.py '{""x"": 1}'
'{x:
# NG
> python .\print_arg1.py '{""x"":1}'
'{x:1}'
# NG
> python .\print_arg1.py '{\"x\":1}'
'{"x":1}'
# NG. If there is a space in the JSON string, only part of it is displayed
> python .\print_arg1.py '{\"x\": 1}'
'{"x":
# ==== Enclosing JSON string in double quotes ====
# OK
>python .\print_arg1.py "{\"x\": 1}"
{"x": 1}
# OK. Escaped even without spaces in the JSON string
>python .\print_arg1.py "{\"x\":1}"
{"x":1}
# OK
>python .\print_arg1.py "{""x"": 1}"
{"x": 1}
# OK. Escaped even without spaces in the JSON string
>python .\print_arg1.py "{""x"":1}"
{"x":1}
In the case of Command Prompt, it seems necessary to escape double quotes with either backslashes or double quotes, and then enclose the entire JSON string in double quotes.
When enclosing JSON strings in double quotes in PowerShell
In PowerShell, while it is correct to enclose the string in single quotes, I will check the behavior when enclosing it in double quotes just in case.
PS > python .\print_arg1.py "{"x": 1}"
{
PS > python .\print_arg1.py "{""x"": 1}"
{x: 1}
PS > python .\print_arg1.py "{""x"":1}"
{x:1}
PS > python .\print_arg1.py "{\"x\": 1}"
{\
Discussion