By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

TypeError: unbound method decode() must be called with JSONDecoder instance as first

P: 4
TypeError: unbound method decode() must be called with JSONDecoder instance as first argument (got PrintJson instance instead)

I am getting this error when I try to parse python object into json.

Using following code:-

Expand|Select|Wrap|Line Numbers
  1. import json
  2. from json import JSONDecoder
  3.  
  4. class TestJson():
  5.     name = None
  6.     pass
  7.  
  8. printJson = PrintJson()
  9. print printJson
  10. print JSONDecoder.decode(printJson) // at this line, getting this error
Sep 5 '15 #1
Share this Question
Share on Google+
5 Replies


P: 4
I tried. This statement JSONDecoder().decode(printJson). But It's not working.
Sep 5 '15 #2

Expert 100+
P: 618
TypeError: unbound method decode() must be called with JSONDecoder instance
Don't know anything about this package but the error message says you use an instance of JSONDecoder, something like
Expand|Select|Wrap|Line Numbers
  1. JD=JSONDecoder()  ## creates class instance
  2. print JD.decode(printJson)
Sep 5 '15 #3

P: 4
Thanks for reply,

I tried but it's not working. Facing below mentioned error, after using with instance "JD".
ERROR:-
Expand|Select|Wrap|Line Numbers
  1. File "C:\Users\jagdeep\test\json_test.py", line 70, in <module>
  2. <json_test.PrintJson object at 0x000000000262AF28>    print jsd.decode(printJson)
  3. Now try to use json decode
  4.   File "C:\Python27\lib\json\decoder.py", line 366, in decode
  5.     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  6. TypeError: expected string or buffer
Sep 6 '15 #4

Expert 100+
P: 618
The error is obvious. It requires a string so do a "print type(printJson)" before the error line and if it is a type that can be converted to a string, you can pass str(printJson) to the function.
Sep 6 '15 #5

P: 4
Expand|Select|Wrap|Line Numbers
  1. import json
  2. from json import JSONDecoder
  3. from json import JSONEncoder
  4.  
  5. class PrintJSON(object):
  6.     name = None
  7.     pass
  8. printJson = PrintJSON()
  9. printJson.name = "This is test"
  10.  
  11.  
  12. jd = JSONDecoder()
  13. print(jd.decode(str(printJson)))
  14.  
  15. But getting error:-
  16. Traceback (most recent call last):
  17.   File "C:/Users/jagdeep/test/json_test2.py", line 15, in <module>
  18.     print(jd.decode(str(printJson)))
  19.   File "C:\Python27\lib\json\decoder.py", line 366, in decode
  20.     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  21.   File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
  22.     raise ValueError("No JSON object could be decoded")
  23. ValueError: No JSON object could be decoded
  24.  
Sep 6 '15 #6

Post your reply

Sign in to post your reply or Sign up for a free account.