2

I have a script that is called from another process that receives an input that is a json string.

import sys
import json

script_name, json_in = sys.argv

my json in looks something like:

'{"key1":"val1", "key2":[val2, val3], "key3":"val4"}'

when i print what is received by python and stored in json_in it looks like:

{key1:val1, key2:[val2, val3], key:val4}

stepping through the pdb when i get to json_in it's value is:

"'{key:val1, key2:[val2, vale3], key:val4}'"

i wasn't having this problem on Ubuntu, but when i use the same script on Windows it no longer works.

The script does work if you copy and paste the exact same input that is being passed to argv. So the only conclusion i've been able to come to is that argv is somehow stripping the double quotes? I haven't found any documentation that might describe why this is happening (especially when it works on Ubuntu).

Does anyone know why its being stripped?

The python function is being called by javascript function, but even if i just call the python scipt from the terminal using

>>> python file_name.py '{"key1":"val1", "key2":[val2, val3], "key3":"val4"}'

I get the same error. If you set the json_in variable to the same value, or call it in the exact same way in the Ubuntu terminal, then there is no problem.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Grant Williams
  • 1,469
  • 1
  • 12
  • 24
  • 1
    How are you printing? Are you using `repr()`? – Sunny Patel May 03 '18 at 16:12
  • @SunnyPatel Do you mean between processes? I'm actually calling the python script from a javascript file using `exec()`. The same process works on Ubuntu, so maybe its a pipelining issue? – Grant Williams May 03 '18 at 16:14
  • If you mean printing in general i've tried writing to a file or command prompt with print, and sys.write. – Grant Williams May 03 '18 at 16:15
  • Show how your program is invoked from the operating system. – Charles Duffy May 03 '18 at 16:27
  • @CharlesDuffy thanks, i've updated the question to further reflect the problem – Grant Williams May 03 '18 at 16:34
  • The Windows shell (`cmd.exe`) doesn't honor single quotes, so that not working is entirely unsurprising. – Charles Duffy May 03 '18 at 16:49
  • @CharlesDuffy okay that makes a lot of sense. Do you have a suggestion for it then? – Grant Williams May 03 '18 at 16:50
  • 1
    Assuming that the accepted answer to [How to escape command line arguments for the `cmd.exe` shell on Windows](https://stackoverflow.com/questions/29213106/how-to-securely-escape-command-line-arguments-for-the-cmd-exe-shell-on-windows) is correct, this would look like `^"{\^"key1\^":\^"val1\^", \^"key2\^":[val2, val3], \^"key3\^":\^"val4\^"}^"`. If that works, let me know and I'll flag this question as a duplicate of the other. – Charles Duffy May 03 '18 at 16:54
  • 1
    (...mind you, `cmd.exe` doesn't actually *do* parsing on Windows -- programs are responsible for parsing their own arguments there rather than having it done by the caller; for typical programs with a `int main(int argc, char** argv)` entry point this is done by the standard C library, but it's not a firm guarantee that any two Windows programs will parse an argument the same way. One more respect in which that whole platform is a %$@#$ mess). – Charles Duffy May 03 '18 at 16:57
  • @CharlesDuffy so excaping it with the `^` did not work at all, but using `\\` worked great, so feel free to flag it – Grant Williams May 03 '18 at 17:04
  • Easiest way is almost certainly to use PowerShell. Single quoted strings work in it. – jpmc26 May 03 '18 at 17:21
  • @jpmc26 that was actually my problem. The PC i wrote this on was ubuntu, then i tested it on a windows machine with the linux subsystem. So i couldnt reproduce the issue. – Grant Williams May 03 '18 at 17:23
  • I found putting single quote arguments with double quotes in front of `"`, `{` and `}` worked. e.g. `python program.py '"{ ""key"": ""value"", ""other"": 10 "}'`. No idea why and annoyingly clunky – Ben Feb 25 '23 at 13:57

0 Answers0