How to parse JSON passed on the command line?

  • Thread starter Anthony Papillion
  • Start date
A

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
 
R

Roy Smith

Anthony Papillion said:
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
 
Y

yupeng zhang

在 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:)
 
D

donarb

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top