Home > Back-end >  Load a dict-like string which includes None value using python
Load a dict-like string which includes None value using python

Time:06-09

Assume there is a string like '{"a": None}'. How to let Python know the existence of None?

This is the error I have been receiving:

JSONDecodeError: Expecting value: line 1 column 7 (char 6)

CodePudding user response:

I'm not sure what you mean exactly by "letting python know of the existence of None," but you can easily convert the string to a dictionary by using the eval function like so:

json_entry = eval('{"a": None}')
print(json_entry["a"])

Output:

 None

CodePudding user response:

Try dumping the format and then load it.

import json
dump = json.dumps('{"a": None}')
load = json.loads(dump)
print(load)
'{"a": None}'
  • Related