After I read the data from the mat file with python, I get a dictionary array. I want to store this dictionary array in a json file, so the json data should be encoded first, so the json.dumps function is used for encoding, but I use json. It is found that there will be problems during the dumps function:
TypeError: Object of type 'bytes' is not JSON serializable
Later, after consulting related materials, I found that many data types of the default encoding function cannot be encoded, so you can write an encoder yourself to inherit jsonencoder, so that it can be encoded.
For example, the above problem is because the json.dumps function found that there are bytes type data in the dictionary, so it cannot be encoded. Just write an encoding class before the encoding function. As long as the data of the bytes type is checked, it will be converted. Into str type.
class MyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, bytes): return str(obj, encoding='utf-8'); return json.JSONEncoder.default(self, obj)
This solved the problem.
Later, similar problems were found during encoding:
TypeError: Object of type 'ndarray' is not JSON serializable
This is the same processing method. When the ndarray data is checked, it is converted into list data:
class MyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() elif isinstance(obj, bytes): return str(obj, encoding='utf-8'); return json.JSONEncoder.default(self, obj)
In this way, the data is encoded.
Put the final code for your reference:
import scipy.io as sio import os import json import numpy as np load_fn = '2%.mat' load_data = sio.loadmat(load_fn) print(load_data.keys()) class MyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() elif isinstance(obj, bytes): return str(obj, encoding='utf-8'); return json.JSONEncoder.default(self, obj) save_fn = os.path.splitext(load_fn)[0] + '.json' file = open(save_fn,'w',encoding='utf-8'); file.write(json.dumps(load_data,cls=MyEncoder,indent=4)) file.close()
Read More:
- TypeError: Decimal type object is not a JSON serialization solution
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int
- Typeerror in Python regular expression: expected string or bytes like object
- [How to Solve] Python TypeError: ‘int‘ object is not subscriptable
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- Python scatter chart error: TypeError: object of type ‘NoneType’ has no len()
- Django Issues: TypeError: “Settings” object is irreversible
- [Solved] Python3.9 Pycropto RSA Error: TypeError: can’t concat str to bytes
- [Solved] pycocotools Install Error: ERROR: Error expected str, bytes or os.PathLike object, not NoneType while executing
- [Solved] Python serializate error: NameError: name ‘JSON’ is not defined
- Python parsing JSON Error: NameError: name ‘false’ is not defined
- ‘coroutine‘ object is not iterable [How to Solve]
- Python Pandas Typeerror: invalid type comparison
- Python error: ‘int’ object is not callable
- [Solved] error: when using the property decorator in Python, an error occurs: typeerror: descriptor ‘setter’ requires a ‘property’ object but
- [Solved] TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
- [Solved] YOLOv5 Model training error: TypeError: new(): invalid data type ‘str’
- [Solved] RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.
- Python TypeError: Unrecognized value type: <class ‘str‘>dateutil.parser._parser.ParserError: Unknow