How to parse JSON passed on the command line?

donarb donarb at nwlink.com
Thu Nov 7 12:11:56 EST 2013


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.



More information about the Python-list mailing list