This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: json serialiser errors with numpy int64
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.7
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 2022-04-11 14:59 by admin. 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/ 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/ 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/ 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)
    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/ in default(self, o)
    178         """
--> 179         raise TypeError(f'Object of type {o.__class__.__name__} '
    180                         f'is not JSON serializable')

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.

msg359582 - (view) Author: Tony Hirst (Tony Hirst) Date: 2020-01-08 11:51
Previously posted issue:
msg359583 - (view) Author: Tony Hirst (Tony Hirst) Date: 2020-01-08 11:53
Argh: previous was incorrect associated issue: correct issue:
msg359584 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-08 12:31
Closing it as duplicate.
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83439
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