Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON Dump and JSON Load
#4
So to expand on this question. My fix worked on my first class but the second class threw an error saying (object not serializable).

So on my second class I went back to this:

warMapStaticDump = json.dumps(warMapStatic, default=lambda x: x.__dict__, indent=4)
And here is the class in question:

class fxWarMapStatic:
    def __init__(self, mapName):
        self.mapName = mapName
        self.regionId = None
        self.eTag = None
        self.scorchedVictoryTowns = None
        self.lastUpdated = None
        self.version = None
        self.mapTextItems = []
Now I can have multiple of these objects. Each one being unique and read in from a separate JSON file.

So to try and import them back in I ran the following:

        with open(warMapStaticDataDump, 'r') as j:
            fxWarConquest.fxWarMapStatics.append(json.loads(j.read()))
This appears to be working its not and I think my ignorance of Python is where I don't understand what its doing.

If you look at the screenshot below its putting ticks around all the variables of the class. Is this because when I use:

default=lambda x: x.__dict__
Its converting my object to dictionaries and when I bring it back in it places everything correctly but as dictionary objects not variables? Once again, I might be butchering all these terms.

I've done a lot of googling and from what I gathered exporting the object out as JSON can be easy but bringing it back in is where things can get complicated and might not be as easy as a single line of code.

[Image: GP0IJBA.png]
Reply


Messages In This Thread
JSON Dump and JSON Load - by foxholenoob - Oct-12-2023, 12:27 AM
RE: JSON Dump and JSON Load - by foxholenoob - Oct-12-2023, 12:46 AM
RE: JSON Dump and JSON Load - by deanhystad - Oct-12-2023, 01:59 AM
RE: JSON Dump and JSON Load - by foxholenoob - Oct-12-2023, 04:01 AM
RE: JSON Dump and JSON Load - by deanhystad - Oct-12-2023, 04:25 AM
RE: JSON Dump and JSON Load - by foxholenoob - Oct-12-2023, 05:39 AM
RE: JSON Dump and JSON Load - by deanhystad - Oct-12-2023, 06:03 AM
RE: JSON Dump and JSON Load - by buran - Oct-12-2023, 07:10 AM
RE: JSON Dump and JSON Load - by foxholenoob - Oct-12-2023, 07:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Json to table format python_student 4 15,665 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Trying to get JSON object in python and process it further Creepy 2 1,204 Oct-24-2024, 08:46 AM
Last Post: buran
  Write json data to csv Olive 6 1,587 Oct-22-2024, 06:59 AM
Last Post: Olive
  get JSON string from URL with Windows credentials shwfgd 0 731 Aug-27-2024, 10:08 PM
Last Post: shwfgd
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,236 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Python beginner needs json help rwskinner 1 751 Aug-22-2024, 05:30 PM
Last Post: deanhystad
  Trying to generating multiple json files using python script dzgn989 4 2,809 May-10-2024, 03:09 PM
Last Post: deanhystad
  encrypt data in json file help jacksfrustration 1 2,497 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 1,840 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 5,289 Jan-23-2024, 07:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020