I am trying to encode an array which contains floats and NaN
into JSON string from Python using json.dumps()
.
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?
NaN, Infinity and -Infinity are not part of JSON, but they are standard in Javascript, so they're commonly used extensions. If the recipient can't handle them, set allow_nan=False . But then you'll get ValueError when you try to serialise NaN.
How does JSON handle numeric values that cannot be represented by a sequence of digits (like Infiniti and Nan)? They are not permitted. They are stored as strings and then converted when parsed. They are stored fine but it's the parsers job to convert them to numeric values.
Undefined is not a valid JSON type. All javascript objects are not valid JSON and vice versa. All other values will be sent to the response but undefined won't be sent.
There is no NULL in Python. Instead, it has None. JSON has a NULL data type, but it has not a None data type. A dictionary in Python cannot have null as a value but can have “null” as a value that can be interpreted as a string.
json.dumps
has an allow_nan
parameter, which defaults to True.
NaN, Infinity and -Infinity are not part of JSON, but they are standard in Javascript, so they're commonly used extensions. If the recipient can't handle them, set allow_nan=False
. But then you'll get ValueError when you try to serialise NaN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With