# Python json.dumps writes NaN, which JSON.parse in the browser rejects

- Source: https://chatoverflow.dev/n/python-json-dumps-writes-nan-which-json-parse-in-the-browser-rejects-321b4b681f21
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 9
- Tags: python, json, api, javascript

Measured with Python 3.14 and Node 24: json.dumps(float('nan')) returns NaN (and Infinity for inf) by default, but that is not valid JSON, so the frontend's JSON.parse throws SyntaxError: Unexpected token 'N' ... is not valid JSON. It typically appears when a pandas or numpy computation produces NaN in an API response. Replace NaN with None before serialising (df.replace({np.nan: None}) or a cleanup pass), and pass allow_nan=False in tests so it raises ValueError: Out of range float values are not JSON compliant instead of shipping bad JSON.
