Python + JSON, what happened to Nothing?

Dumping and loading a dict using None as key results in a dict with "null" as the key.

The values ​​are not affected, but things get even worse if there is actually a "null" string key.

What am I doing wrong here? Why can't I serialize / deserialize a dict with the No keys?

Example

>>> json.loads(json.dumps({'123':None, None:'What happened to None?'})) {u'123': None, u'null': u'What happened to None?'} >>> json.loads(json.dumps({'123':None, None:'What happened to None?', 'null': 'boom'})) {u'123': None, u'null': u'boom'} 
+6
json python dictionary
source share
3 answers

JSON objects are string maps for values. If you try to use a different type of key, they are converted to strings.

 >>> json.loads(json.dumps({123: None})) {'123': None} >>> json.loads(json.dumps({None: None})) {'null': None} 
+17
source share

According to the specification, None not a valid key. This will mean a JSON object expression that looks like

 { ..., null: ..., ... } 

which is invalid (i.e. cannot be generated using a syntax diagram.)

Perhaps the JSON module should have thrown an exception during serialization instead of silently generating a string representation of the value.

EDIT Just saw that the behavior of the module is documented (somewhat implicitly):

If skipkeys True (default: False), then instead of TypeError keys, keys that do not have a base type (str, unicode, int, long, float, bool, None) will be skipped.

so it seems that this behavior is intentional (I still doubt it considering the current JSON specification).

+10
source share

As shown in @ dan04, None is converted to "null".
Everything is fine, the value is stored in a dict with "null:" What happened to None? ""

But then another key appeared with a "zero".
So, the old value with None / 'null'-Key ("What happened to None?") Is overwritten with an arrow.

-one
source share

All Articles