Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to parse JSON passed on the command line?

48 views
Skip to first unread message

Anthony Papillion

unread,
Nov 6, 2013, 10:53:09 PM11/6/13
to pytho...@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

Chris Rebert

unread,
Nov 6, 2013, 11:06:56 PM11/6/13
to Anthony Papillion, Python
On Wed, Nov 6, 2013 at 7:53 PM, Anthony Papillion <papi...@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"}

You probably want
getargfromcli.py '{"url":"http://www.google.com"}'
instead, so that your string of JSON is treated literally by the shell.

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

No, that's not JSON anymore! All the required quotation marks have
gone missing. The shell ate them.

Regards,
Chris

Roy Smith

unread,
Nov 6, 2013, 11:12:27 PM11/6/13
to
In article <mailman.2117.1383796...@python.org>,
Anthony Papillion <papi...@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

yupeng zhang

unread,
Nov 7, 2013, 1:09:49 AM11/7/13
to
在 2013年11月7日星期四UTC+8上午11时53分09秒,Anthony Papillion写道:
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:)

donarb

unread,
Nov 7, 2013, 12:11:56 PM11/7/13
to
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.
0 new messages