1

I am trying to prints json in readable form. I already checked previous threads and tried out.

using

JSON.stringify(response)

gives error:

NameError: name 'JSON' is not defined

Using

response = json.loads(urllib.urlopen(url).read())
parsed = json.loads(response)
print json.dumps(parsed, indent=4, sort_keys=True)

gives error:

Traceback (most recent call last):
  File "p6.py", line 15, in <module>
    parsed = json.loads(response)
  File "/usr/lib/python2.7/json/__init__.py", line 328, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
1
  • 2
    JSON.stringify is not even Python. It's javascript. Commented Aug 14, 2014 at 5:53

1 Answer 1

3

You get

NameError: name 'JSON' is not defined

because the first snippet is in JavaScript, not in Python.

As to the second snippet, you are calling json.loads() twice:

response = json.loads(urllib.urlopen(url).read())  # calling once
parsed = json.loads(response)                      # calling twice

Just call it once (and ensure that what you get from the HTTP server is actually JSON).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.