bigjson icon indicating copy to clipboard operation
bigjson copied to clipboard

Dealing with NaN in json files

Open lennertcl opened this issue 1 year ago • 1 comments

I am trying to read a file which contains data with NaN values, e.g. {"a": NaN}. I know this is not valid JSON, however I would like to know if there is a workaround that allows me to read this file anyway without getting this error:

Exception: Unexpected bytes! Value 'N' Position 170

Should I just replace all occurrences of NaN with null or "NaN" first or is there a better way to handle this?

lennertcl avatar Nov 08 '23 10:11 lennertcl

For reference, I managed to change the file using this code:

with open("original_file.json", "rb") as f:
    with open("updated_file.json", "wb") as f_out:
        while (byte := f.read(1)):
            if byte == b":":
                val = f.read(4)
                if val == b" NaN":
                    f_out.write(b': "NaN"')
                else: 
                    f_out.write(byte)
                    f_out.write(val)
            else:
                f_out.write(byte)

lennertcl avatar Nov 08 '23 10:11 lennertcl