pass a json string as an argument to Python script causes quotes problems
Your code will work as expected if you change how you assign your input variable. Change this line:
inp = parser.parse_args()
to
inp = args.inputstring
parse_args()
returns an argparse.Namespace
object, so you need to retrieve the input from that object prior to passing it to the parser. In addition, you will need to escape the double quotes in your shell command. I get the expected output with the above change run with:
python myscript.py -i "{ \"Employees\": \"name name\"}"
You should escape the internal double quotes in the JSON:
python myscript.py -i "{ \"Employees\": \"name name\"}"
But looking at your code, there are a couple of other reasons it may not be working:
you're assigning
args
andinp
the same value, return value of the function call:parser.parse_args()
. Perhaps you meant to assigninp = args.inputstring
you've tagged the question as python2 but looking at your
print(...)
statement, it looks like you've written for python3