44

I am trying to parse JSON from Python. I recently started working with Python so I followed some stackoverflow tutorial how to parse JSON using Python and I came up with below code -

#!/usr/bin/python
import json

j = json.loads('{"script":"#!/bin/bash echo Hello World"}')
print j['script']

But whenever I run the above code, I always get this error -

Traceback (most recent call last):
  File "json.py", line 2, in <module>
    import json
  File "/cygdrive/c/ZookPython/json.py", line 4, in <module>
    j = json.loads('{"script":"#!/bin/bash echo Hello World"}')
AttributeError: 'module' object has no attribute 'loads'

Any thoughts what wrong I am doing here? I am running cygwin in windows and from there only I am running my python program. I am using Python 2.7.3

And is there any better and efficient way of parsing the JSON as well?

Update:-

Below code doesn't work if I remove the single quote since I am getting JSON string from some other method -

#!/usr/bin/python

import json

jsonStr = {"script":"#!/bin/bash echo Hello World"}

j = json.loads(jsonStr)
shell_script = j['script']
print shell_script

So before deserializing how to make sure, it has single quote as well?

This is the error I get -

Traceback (most recent call last):
  File "jsontest.py", line 7, in <module>
    j = json.loads(jsonStr)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • what do you mean is there a better way? in python there should be one clear way to do things... and `json.loads` is it. – Joran Beasley Nov 19 '13 at 21:48
  • Also see https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at – PM 2Ring Nov 07 '17 at 07:04

2 Answers2

127
File "json.py", line 2, in <module>
  import json

This line is a giveaway: you have named your script "json", but you are trying to import the builtin module called "json", since your script is in the current directory, it comes first in sys.path, and so that's the module that gets imported.

You need to rename your script to something else, preferrably not a standard python module.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • Ok.. let me try that out.. I will rename my script to jsontest.py and delete the old json.py from the ZookPython directory. – AKIWEB Nov 19 '13 at 21:51
  • One last thing.. Again, its a silly question. Actually, I am getting my JSON document from some other method, so the actual JSON document that I am getting is without single quote so it will be like this `jsonStr = {"script":"#!/bin/bash echo Hello World"}` so if I use the above JSON String to deserialize it and extract script portion, then it doesn't work. Something like this - `j = json.loads(jsonStr) print j['script']` will give me error. So now how do I add that single quote after getting the actual json string from a method and then deserialize it properly? – AKIWEB Nov 19 '13 at 22:21
  • 1
    If you already deserialized the JSoN, then it's not json anymore, it's just a plain ol' `dict`. – SingleNegationElimination Nov 19 '13 at 22:45
  • I see, but I believe it is not deserializing it without adding any single quote.. I have updated my question with an example.. – AKIWEB Nov 19 '13 at 22:48
  • You've already accepted an answer, to your original question. If you are stuck on a new problem, consider asking a new question; but as I said, without the single quotes, it's not json anymore, its just a python dict now. – SingleNegationElimination Nov 19 '13 at 22:54
  • So this implies you can't name a directory = python package like a standard one? Or there's no way to precise "standard package"? Because I've tried to make a folder named "`json`" in my "`views`" directory and I've got the same problem – Olivier Pons Apr 02 '16 at 17:14
  • @Olivier Pons: if the fully qualified import path would be something other than just `json`, say `myapp.views.json`, you can work around it by using [relative imports](https://docs.python.org/2/whatsnew/2.5.html#pep-328-absolute-and-relative-imports), but I would not do that. – SingleNegationElimination Apr 04 '16 at 18:22
  • 3
    Haaaa I did the EXACT same thing and ended up here. – boson Jun 18 '16 at 01:27
9

It looks like you have a json.py module which is not part of the Standard Library. Not sure what ZookPython is. Try renaming ZookPython directory (or just json.py) and re-run.

Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • I have a directory called ZookPython in which I have json.py file which contains my above code. so you are saying, I cannot run my python code from any directory? – AKIWEB Nov 19 '13 at 21:49
  • 1
    You are getting Python confused. In order to use the _standard_ `json.py` module you have to load it. Rename _your_ file to some other name and it should work. – Alexander L. Belikoff Nov 19 '13 at 21:51