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


Groups > comp.lang.python > #58627 > unrolled thread

How to parse JSON passed on the command line?

Started byAnthony Papillion <papillion@gmail.com>
First post2013-11-06 21:53 -0600
Last post2013-11-07 09:11 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#58627 — How to parse JSON passed on the command line?

FromAnthony Papillion <papillion@gmail.com>
Date2013-11-06 21:53 -0600
SubjectHow to parse JSON passed on the command line?
Message-ID<mailman.2117.1383796399.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#58630

FromRoy Smith <roy@panix.com>
Date2013-11-06 23:12 -0500
Message-ID<roy-CDE312.23122706112013@news.panix.com>
In reply to#58627
In article <mailman.2117.1383796399.18130.python-list@python.org>,
 Anthony Papillion <papillion@gmail.com> wrote:

> 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}

Which is not valid JSON.  You lost the quotes.  I suspect you want to

[toc] | [prev] | [next] | [standalone]


#58631

Fromyupeng zhang <universeroc@gmail.com>
Date2013-11-06 22:09 -0800
Message-ID<a01c3474-68b0-436c-8e56-94b89ce5f9bc@googlegroups.com>
In reply to#58627
在 2013年11月7日星期四UTC+8上午11时53分09秒,Anthony Papillion写道:
> 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

Hi Anthony Papillion.
I'm fresh to Python, but I do love its short simple and graceful.
I've solved your problem. You could try the code below:

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

AS command line will strip your ".
From the Python document, we could get the info as:
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')

the json.loads' argument should be string. Try it:)

[toc] | [prev] | [next] | [standalone]


#58672

Fromdonarb <donarb@nwlink.com>
Date2013-11-07 09:11 -0800
Message-ID<6d7ab7b9-4104-4a2e-8dc2-68977410dbb4@googlegroups.com>
In reply to#58631
On Wednesday, November 6, 2013 10:09:49 PM UTC-8, yupeng zhang wrote:
> 
> Hi Anthony Papillion.
> 
> I'm fresh to Python, but I do love its short simple and graceful.
> I've solved your problem. You could try the code below:
> 
> getargfromcli.py "\"{'url':'http://www.google.com'}\""
> 
> AS command line will strip your ".
> 
> From the Python document, we could get the info as:
> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
> the json.loads' argument should be string. Try it:)

That's not going to work, JSON strings must use double quotes, which you've rewritten as single quotes. The correct way (as shown previously) is to wrap the entire string in single quotes, thus preserving the double quotes inside.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web