Ask a Question

json deserialization

SOLVED
MBB
Occasional Contributor

json deserialization

I have a dictionary of named objects that I've serialized.  When I deserialize it in TC, it seems to understand that it's a dictionary and it shows the names as its keys, but the objects don't show up as objects.  It looks to me like each one is itself a dictionary.  This is not how the same code works for me in PyCharm.

 

With your permission, I'll show the serialized object and then the code:

{
    "company1": {
        "py/object": "__main__.Company",
        "number": "1"
    },
    "company2": {
        "py/object": "__main__.Company",
        "number": "2"
    },
    "company3": {
        "py/object": "__main__.Company",
        "number": "3"
    }
}
class Company:
    def __init__(self, number):
        self.number = number

 

r = open("c:\\temp\\test.json", 'r')
todecode = r.read()
decoded = jsonpickle.decode(todecode)
print(decoded["company1"].number)
print(decoded["company2"].number)
print(decoded["company3"].number)

 

That code works in PyCharm and outputs 

1
2
3

 

The same thing in TC gives me

AttributeError: 'dict' object has no attribute 'number' 17:54:40 Normal 0.00

 

It seems to be deserializing the dictionary into tuples where the key is correct but the value is a dict, rather than a Company object.

1 ACCEPTED SOLUTION

Accepted Solutions
mattb
Staff

Re: json deserialization

I wonder if its due to parameter mismatch.

I think the issue resides in this code block: 

class Company:
    def __init__(self, number):
        self.number = number


Does this code work in another IDE, not PyCharm? 

View solution in original post

6 REPLIES 6
Marsha_R
Community Hero

Re: json deserialization

Just a guess here, since I don't know python

 

You defined 

class Company:
    def __init__(self, number):
        self.number = number

Do you need to print self .number instead of number? 

Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
MBB
Occasional Contributor

Re: json deserialization

Thanks for answering.

 

I don't think that's the problem, especially given that the code works as-is when I run it in PyCharm.  In TC, when I look at the dictionary in the Watch window I see not "Company1" and a Company object, but rather "Company1" and what appears to be a dict.

mattb
Staff

Re: json deserialization

I wonder if its due to parameter mismatch.

I think the issue resides in this code block: 

class Company:
    def __init__(self, number):
        self.number = number


Does this code work in another IDE, not PyCharm? 

View solution in original post

MBB
Occasional Contributor

Re: json deserialization

I'll look into the matter of the constructor parameter, as you suggest.  Regarding another Python IDE, I'm open to suggestion.  Can you recommend one?

MBB
Occasional Contributor

Re: json deserialization

I’ve tried running the code using Visual Studio 2019’s Python.  It works there too.

 

I also tried adding a “default constructor” (if that’s what it’s called in Python) with just the self parameter.  Didn’t help.  It still doesn't work in TC.

chriscc
Occasional Contributor

Re: json deserialization

MBB,

 

Try setting the deserialized JSON object to a variable like this:

 

      var obj = {"company1": {"py/object": "__main__.Company", "number": "1"},

                 "company2": {"py/object": "__main__.Company", "number": "2"},

                 "company3": {"py/object": "__main__.Company","number": "3"}};

 

 Then call the properties like this:

 

      var num1 = obj.company1.number;

      var num2 = obj.company2.number;

      var num3 = obj.company3.number;

Chris
cancel
Showing results for 
Search instead for 
Did you mean: