How to parse JSON passed on the command line?

Anthony Papillion papillion at gmail.com
Wed Nov 6 22:53:09 EST 2013


Hello Everyone,

I'm writing a little helper script in Python that will access a JSON
formatted argument from the shell when it's called. The parameter will
look like this:

{"url":"http://www.google.com"}

So, if my program is called "getargfromcli.py" the call will look like this:

getargfromcli.py {"url":"http://www.google.com"}

In the case above, I assume my JSON string will be argv[1]. In fact,
when I do

print sys.argv[1]

It works as expected and prints out the JSON string as expected like
this: {url:http://www.google.com}

Now, for the harder part. When I try to PARSE this JSON using this code:

json_string = json.loads(sys.argv[1])

I get an error saying that "No JSON object could be decoded".  Even
though this looks like valid JSON and was generated by a JSON generator.

Can anyone tell me what I'm doing wrong? Basically, I want to eventually
get the value of url into a string.

Thanks!
anthony




More information about the Python-list mailing list