4

I would like to take a Python object and serialize/deserialize to/from JSON. The object has numpy arrays inside it:

class Student(object):
    name = ""
    age = 0
    major = ""
    teacher = ""
    #tuple1 = (1,2,3)
    #tuple2 = ("a","b","c")
    ndarr = ""

    def __init__(self, name, age, major):
        self.name = name
        self.age = age
        self.major = major

I am currently using jsonpickle to serialize it to json.

frozen = jsonpickle.encode(obj)
print "jsonpickle serialized object: " + frozen

However, after I serialize it, the numpy array looks weird:

jsonpickle serialized object:

{"py/object": "Student.Student", "age": 21, "major": "eecs", "ndarr": {"py/reduce": [{"py/object": "__builtin__.builtin_function_or_method"}, {"py/tuple": [{"py/type": "numpy.ndarray"}, {"py/tuple": [0]}, "b"]}, {"py/tuple": [1, {"py/tuple": [2, 2]}, {"py/reduce": [{"py/type": "numpy.dtype"}, {"py/tuple": ["f8", 0, 1]}, {"py/tuple": [3, "<", null, null, null, -1, -1, 0]}, null, null]}, true, {"py/b64": "2MfF58x/AADYx8XnzH8AAAAAAAAAAAAAAAAAAAAAAAA=\n"}]}, null, null]}, "name": "larry"}

is there an easy way to serialize numpy array objects that are inside of another object? I find the first answer inside this post to work, but it is not within an object: NumPy array is not JSON serializable

mjpablo23
  • 681
  • 1
  • 7
  • 23
  • Maybe this post can help: https://stackoverflow.com/questions/26646362/numpy-array-is-not-json-serializable – hmaarrfk Oct 31 '18 at 14:43

0 Answers0