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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jagdeep
    New Member
    • Sep 2015
    • 4

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

    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:-

    Code:
    import json
    from json import JSONDecoder
    
    class TestJson():
        name = None
        pass
    
    printJson = PrintJson()
    print printJson
    print JSONDecoder.decode(printJson) // at this line, getting this error
    Last edited by Rabbit; Sep 5 '15, 04:36 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Jagdeep
    New Member
    • Sep 2015
    • 4

    #2
    I tried. This statement JSONDecoder().d ecode(printJson ). But It's not working.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      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
      Code:
      JD=JSONDecoder()  ## creates class instance
      print JD.decode(printJson)

      Comment

      • Jagdeep
        New Member
        • Sep 2015
        • 4

        #4
        Thanks for reply,

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

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          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.

          Comment

          • Jagdeep
            New Member
            • Sep 2015
            • 4

            #6
            Code:
            import json
            from json import JSONDecoder
            from json import JSONEncoder
            
            class PrintJSON(object):
                name = None
                pass
            printJson = PrintJSON()
            printJson.name = "This is test"
            
            
            jd = JSONDecoder()
            print(jd.decode(str(printJson)))
            
            But getting error:-
            Traceback (most recent call last):
              File "C:/Users/jagdeep/test/json_test2.py", line 15, in <module>
                print(jd.decode(str(printJson)))
              File "C:\Python27\lib\json\decoder.py", line 366, in decode
                obj, end = self.raw_decode(s, idx=_w(s, 0).end())
              File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
                raise ValueError("No JSON object could be decoded")
            ValueError: No JSON object could be decoded

            Comment

            Working...