While Python supports dictionary keys of an arbritary type, JSON does not. The JSON spec clearly states that keys must be strings. Thus, any library that decodes JSON will not have an option to attempt to decode keys as another type (such as a numnber).
However it is easy to to this yourself from Python.
json_Str = '{"1": "foo", "2": "bar"}'
if __name__ == '__main__':
x = json.loads(json_Str)
result = {}
for k, v in json.loads(json_Str).items():
try:
as_int = int(k)
result[as_int] = v
except ValueError:
result[k] = v
print(result)
assert(type(result.keys()[0]) is int)
If you need to do this on multiple JSON structures, then you can also make your own custom decoding hook. This will allow you to add the ability for "Python to support JSON interger keys".
This approach also replaces the dictionary keys in place, which is slightly more memory efficient if you are working with large datasets.
import json
json_Str = '{"1": "foo", "2": "bar", "4": [1, 2, 3]}'
def int_key_hook(obj):
if type(obj) is not dict:
return obj
for key in obj.keys():
try:
as_int = int(key)
obj[as_int] = obj[key]
del obj[key]
except ValueError:
pass
return(obj)
my_decoder = json.JSONDecoder(object_hook=int_key_hook)
if __name__ == '__main__':
x = my_decoder.decode(json_Str)
print(x)
assert(type(x.keys()[1]) is int)