Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58627

How to parse JSON passed on the command line?

Date 2013-11-06 21:53 -0600
From Anthony Papillion <papillion@gmail.com>
Subject How to parse JSON passed on the command line?
Newsgroups comp.lang.python
Message-ID <mailman.2117.1383796399.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How to parse JSON passed on the command line? Anthony Papillion <papillion@gmail.com> - 2013-11-06 21:53 -0600
  Re: How to parse JSON passed on the command line? Roy Smith <roy@panix.com> - 2013-11-06 23:12 -0500
  Re: How to parse JSON passed on the command line? yupeng zhang <universeroc@gmail.com> - 2013-11-06 22:09 -0800
    Re: How to parse JSON passed on the command line? donarb <donarb@nwlink.com> - 2013-11-07 09:11 -0800

csiph-web