I am trying to encode an array which contains floats and
NaN into JSON string from Python using
But the encoded JSON string is not being decoded successfully in PHP. Is the
NaN causing this problem? How can I work around this situation?
json.dumps has an
allow_nan parameter, which defaults to True.
allow_nan=False. But then you’ll get ValueError when you try to serialise NaN.
The simplejson package on which Python’s standard json package is based moves more quickly, and handles this situation.
NaN is not valid JSON, and the
ignore_nan flag will handle correctly all
import simplejson as json json.dumps(thing, ignore_nan=True)
default parameter will allow simplejson to parse your datetimes correctly.
json.dumps(response, ignore_nan=True, default=datetime.datetime.isoformat)
simplejson can be installed with pip.
pip install simplejson
NaN is not a valid JSON symbol, see the spec at http://json.org/
Your encoder should probably have encoded the NaN as