Within the following class I am trying to save some state information to a json file, however when I attempt to save a dictionary I come across a TypeError: '<li>...stuff...</li>' is not JSON serializable
class Save(object):
def __init__(self, MainFrameDict):
super(Save, self).__init__()
self.MainFrameDict = MainFrameDict
import pdb; pdb.set_trace()
self.writeJson()
def writeJson(self):
self.json_state_file = os.path.join(self.MainFrameDict['item_folder'],
self.MainFrameDict['itemNumber']+'.json')
with open(self.json_state_file,'wb') as f:
json.dump(self.MainFrameDict['currentItemInfo'], f)
self.printJsonStateFile()
#import pdb; pdb.set_trace()
def printJsonStateFile(self):
with open(self.json_state_file,'rb') as f:
json_data = json.loads(f)
Within the dictionary that I am trying to save:
(Pdb) print(self.MainFrameDict['currentItemInfo'].keys())
['image_list', 'description', 'specs']
(Pdb) print(self.MainFrameDict['currentItemInfo']['description'])
Run any time in the comfort of your own home. Deluxe treadmill
features 9 programs; plug in your MP3 player to rock your workout!
<ul>
<li>Horizon T101 deluxe treadmill</li>
<li>55" x 20" treadbelt</li>
<li>9 programs</li>
<li>Fan</li>
<li>Motorized incline to 10%</li>
<li>Up to 10 mph</li>
<li>Surround speakers are compatible with your MP3 player (not
included)</li>
<li>71"L x 33"W x 55"H</li>
<li>Wheels for mobility</li>
<li>Folds for storage</li>
<li>Weight limit: 300 lbs.</li>
<li>Assembly required</li>
<li>Limited warranty</li>
<li>Made in USA</li>
</ul>
(Pdb) print type(self.MainFrameDict['currentItemInfo']['description'])
<class 'bs4.BeautifulSoup'>
The traceback that I am trying to figure out:
Traceback (most recent call last):
File "display_image.py", line 242, in onNewItemButton
Save(MainFrame.__dict__)
File "display_image.py", line 20, in __init__
self.writeJson()
File "display_image.py", line 24, in writeJson
json.dump(self.MainFrameDict['currentItemInfo'], f)
File "C:\Python27\Lib\json\__init__.py", line 189, in dump
for chunk in iterable:
File "C:\Python27\Lib\json\encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "C:\Python27\Lib\json\encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "C:\Python27\Lib\json\encoder.py", line 442, in _iterencode
o = _default(o)
File "C:\Python27\Lib\json\encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Run any time in the comfort of your own home. Deluxe treadmill
features 9 programs; plug in your MP3 player to rock your workout!
<ul>
<li>Horizon T101 deluxe treadmill</li>
<li>55" x 20" treadbelt</li>
<li>9 programs</li>
<li>Fan</li>
<li>Motorized incline to 10%</li>
<li>Up to 10 mph</li>
<li>Surround speakers are compatible with your MP3 player (not
included)</li>
<li>71"L x 33"W x 55"H</li>
<li>Wheels for mobility</li>
<li>Folds for storage</li>
<li>Weight limit: 300 lbs.</li>
<li>Assembly required</li>
<li>Limited warranty</li>
<li>Made in USA</li>
</ul>
is not JSON serializable
Docs/Posts looked at:
- https://docs.python.org/2/library/json.html
- What is the correct JSON content type?
- Python serializable objects json
- Python serializable objects json
- is not JSON serializable
- Python sets are not json serializable
- Python serializable objects json
- How to overcome "datetime.datetime not JSON serializable"?
- JSON datetime between Python and JavaScript
- How to overcome "datetime.datetime not JSON serializable"?
- JSON serialization of Google App Engine models
I am not sure if this is because it is nested, or if there is an issue with encoding/decoding. What exactly am I looking for and what am I not understanding? Is there a way to determine the encoding of an item?