I have a pandas array with np.NaN's in it that I convert to a dictionary for JSON
json_data = {"freq" : list(df["Data"])}
I get ready to load to a web page via flask jsonify
@dashboard.route('/command', methods = ['GET', 'POST'])
def command():
[...]
return jsonify(json_data)
and on the javascript side I get ready to read it.
$.ajax({
url: '/dashboard/command',
type: 'POST',
data: data,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
if (!msg.error) {
updatePlot(msg);
}
else {
alert(msg.error);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("There has been an error retrieving your data: \n\n" + errorThrown);
},
complete: function() {
$("#update-plot").prop('disabled', false); // Re-enable button
$("#update-plot-loading").addClass('invisible'); // Hide loading animation
}
And that is where it dies if I load that web page. It says that it has a bad token "N" in the json. If I replace the NaN's before trying to send, e.g.
df["Data"].replace(np.NaN, -999.999)
and all is fine and the javascript can continue. I'm aware that the NaN's should be "null" on the javascript side, so what I do at the moment (ugh) is then convert the -999.999's to "null"... But gosh, shouldnt there be a way to send missing data directly?
Any suggestions? I have tried many combinations of NaN, "NaN", None, etc. But maybe I missed one. I checked the output of jsonify on the python said and it seems to be a valid response to send over.
Thanks, T.