classification
Title: json serialiser errors with numpy int64
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: json fails to serialise numpy.int64
View: 24313
Assigned To: Nosy List: Tony Hirst, xtreak
Priority: normal Keywords:

Created on 2020-01-08 11:17 by Tony Hirst, last changed 2020-01-08 12:31 by xtreak. This issue is now closed.

Messages (5)
msg359579 - (view) Author: Tony Hirst (Tony Hirst) Date: 2020-01-08 11:18
````
import json
import numpy as np

json.dumps( {'int64': np.int64(1)})

TypeError: Object of type int64 is not JSON serializable 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-67-e2b056f3a557> in <module>
----> 1 json.dumps( {'int64': np.int64(1)})

/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    229         cls is None and indent is None and separators is None and
    230         default is None and not sort_keys and not kw):
--> 231         return _default_encoder.encode(obj)
    232     if cls is None:
    233         cls = JSONEncoder

/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py in default(self, o)
    177 
    178         """
--> 179         raise TypeError(f'Object of type {o.__class__.__name__} '
    180                         f'is not JSON serializable')
    181 

TypeError: Object of type int64 is not JSON serializable

````
msg359580 - (view) Author: Tony Hirst (Tony Hirst) Date: 2020-01-08 11:21
Apols - this is probably strictly a numpy issue.

See: https://github.com/numpy/numpy/issues/12481
msg359582 - (view) Author: Tony Hirst (Tony Hirst) Date: 2020-01-08 11:51
Previously posted issue: https://bugs.python.org/issue22107
msg359583 - (view) Author: Tony Hirst (Tony Hirst) Date: 2020-01-08 11:53
Argh: previous was incorrect associated issue: correct issue: https://bugs.python.org/issue24313
msg359584 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-08 12:31
Closing it as duplicate.
History
Date User Action Args
2020-01-08 12:31:36xtreaksetstatus: open -> closed

superseder: json fails to serialise numpy.int64

nosy: + xtreak
messages: + msg359584
resolution: duplicate
stage: resolved
2020-01-08 11:53:11Tony Hirstsetmessages: + msg359583
2020-01-08 11:51:52Tony Hirstsetmessages: + msg359582
2020-01-08 11:21:40Tony Hirstsetmessages: + msg359580
2020-01-08 11:18:40Tony Hirstsetmessages: + msg359579
2020-01-08 11:17:19Tony Hirstcreate