Encoding NaN in JSON

M

Miki Tebeka

Greetings,

I'm trying to find a way to have json emit float('NaN') as 'N/A'.
I can't seem to find a way since NaN is a float, which means overriding "default" won't help.

Any simple way to do this?

Thanks,
 
M

Miki Tebeka

I'm trying to find a way to have json emit float('NaN') as 'N/A'.
No. There is no way to represent NaN in JSON. It's simply not part of the
specification.
I know that. I'm trying to emit the *string* 'N/A' for every NaN.
 
J

Johann Hibschman

Miki Tebeka said:
I know that. I'm trying to emit the *string* 'N/A' for every NaN.

Easiest way is probably to transform your object before you try to write
it, e.g.

def transform(x):
if isinstance(x, dict):
return dict((k, transform(v)) for k, v in x.items())
elif isinstance(x, list) or isinstance(x, tuple):
return [transform(v) for v in x]
elif isinstance(x, float) and x != x:
return 'N/A'
else:
return x

Then just use

json.dumps(transform(x))

rather than just

json.dumps(x)
 
M

Miki Tebeka

I'm trying to find a way to have json emit float('NaN') as 'N/A'.
Easiest way is probably to transform your object before you try to write
Yeah, that's what I ended up doing. Wondered if there's a better way ...

Thanks,
 
D

Dave Angel

Miki Tebeka said:
I know that. I'm trying to emit the *string* 'N/A' for every NaN.

Easiest way is probably to transform your object before you try to write
it, e.g.

def transform(x):
if isinstance(x, dict):
return dict((k, transform(v)) for k, v in x.items())
elif isinstance(x, list) or isinstance(x, tuple):
return [transform(v) for v in x]
elif isinstance(x, float) and x != x:
return 'N/A'
else:
return x

Note that for a self-referencing object, this function might run
"forever," or until it runs out of stack. The programmer is likely to
know about the possibility, but just in case ...
 
R

Roland Koebler

Hi,
Yeah, that's what I ended up doing. Wondered if there's a better way ...
yes, there is: subclass+extend the JSON-encoder, see pydoc json.

e.g.:
class JsonNanEncoder(json.JSONEncoder):
def default(self, obj):
if some-check-if-obj-is-NaN:
return 'NaN'
return json.JSONEncoder.default(self, obj)

Roland
 
M

Miki Tebeka

[Roland]
yes, there is: subclass+extend the JSON-encoder, see pydoc json.
Please read the original post before answering. What you suggested does not work since NaN is of float type.
 
R

Roland Koebler

Hi,
Please read the original post before answering. What you suggested does not work since NaN is of float type.
ok, right, default does not work this way.
But I would still suggest to extend the JSON-encoder, since that is
quite simple (see sourcecode of JSON module); as a quickhack, you
could even monkey patch json.encoder.floatstr with a wrapper which
returns "N/A" for NaN. (I've tested it: It works.)

But: If you only need NaN and inf, and are ok with 'NaN' instead of 'N/A',
you can simply use the json module. See pydoc json:

If allow_nan is True, then NaN, Infinity, and -Infinity will be
encoded as such. This behavior is not JSON specification compliant,
but is consistent with most JavaScript based encoders and decoders.
Otherwise, it will be a ValueError to encode such floats.
'Infinity'


Roland
 
C

Chris Angelico

[Roland]
yes, there is: subclass+extend the JSON-encoder, see pydoc json.
Please read the original post before answering. What you suggested does not work since NaN is of float type.

You may be able to override a bit more of the code, though. Check out
Lib/json/encoder.py for the implementation, and have a look at the
floatstr() internal function; unfortunately you can't simply subclass
and override that, but perhaps overriding iterencode (which is where
floatstr is defined) would do the job.

ChrisA
 
C

Chris Angelico

as a quickhack, you
could even monkey patch json.encoder.floatstr with a wrapper which
returns "N/A" for NaN. (I've tested it: It works.)

Wait... you can do that? It's internal to iterencode, at least in
Python 3.3 and 2.7 that I'm looking at here. Can you share your code
please? I'd like to try that! When I first looked at the docstring, I
was thinking "Ah, can I override the bit that emits NaN to return
\"N/A\" instead?", but the code made me think that's not possible.

ChrisA
 
R

Roland Koebler

Wait... you can do that? It's internal to iterencode, at least in
Python 3.3 and 2.7 that I'm looking at here.
In Python 2.6 it wasn't internal to iterencode; in Python 2.7 and 3.x
you probably would have to monkey-patch iterencode. (In addition, patching
floatstr alone wouldn't be enough in 3.x and probably 2.7, since you also
have to make sure that the C-extension is not used here.)

BUT: Keep in mind that monkey-patches are problematic, and should be
avoided (or used very carefully) in production code. So, better
replace the complete encoder.py or use your own patched version
of the complete json-module.

Roland
 
R

Robert Kern

You understand that this will result in a chunk of text that is not JSON?
Other JSON readers won't be able to read it.

I think he means something like this:
'["N/A"]'

Not

'[N/A]'

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
C

Chris “Kwpolska†Warrick

Why not use 'NaN' instead? It seems to be even more semantically
similar...

Because there is no NaN in JSON? Unless you mean a string, which
makes no semantical sense and is human-oriented and not
machine-oriented.
 
G

Grant Edwards

Because there is no NaN in JSON? Unless you mean a string, which
makes no semantical sense and is human-oriented and not
machine-oriented.

The OP asked for a string, and I thought you were proposing the string
'null'. If one is to use a string, then 'NaN' makes the most sense,
since it can be converted back into a floating point NaN object.

I infer that you were proposing a JSON null value and not the string
'null'?
 
C

Chris “Kwpolska†Warrick

The OP asked for a string, and I thought you were proposing the string
'null'. If one is to use a string, then 'NaN' makes the most sense,
since it can be converted back into a floating point NaN object.

I infer that you were proposing a JSON null value and not the string
'null'?

Not me, Wayne Werner proposed to use the JSON null value. I parsed
the backticks (`) used by him as a way to delimit it from text and not
as a string.

PS.
On 2013-04-19, Chris ???Kwpolska??? Warrick <[email protected]> wrote:

Is Unicode support so hard, especially in the 21st century?
 
W

Wayne Werner

Not me, Wayne Werner proposed to use the JSON null value. I parsed
the backticks (`) used by him as a way to delimit it from text and not
as a string.

That was, in fact, my intention. Though it seems to me that you'll have to
suffer between some sort of ambiguity - in Chrome, at least,
`Number(null)` evaluates to `0` instead of NaN. But `Number('Whatever')`
evaluates to NaN. However, a JSON parser obviously wouldn't be able to
make the semantic distinction, so I think you'll be left with whichever
API makes the most sense to you:

NaN maps to null

or

NaN maps to "NaN" (or any other string, really)


Obviously you're not limited to these particular choices, but they're
probably the easiest to implement and communicate.

HTH,
-W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top