Tag Archives: NameError: name ‘null’ is not defined

NameError: name ‘null’ is not defined [How to Solve]

NameError: name ‘null’ is not defined

When running the interface test script today, an error NameError was encountered: name ‘null’ is not defined. Cause: there is a null value when converting a string to a dictionary

ret = '{"createdAt":"","updatedAt":"", "dataSets":null}'
ret = eval(ret)
print(ret)

out:
ret = eval(ret)
  File "<string>", line 1, in <module>
NameError: name 'null' is not defined

The solution is to use the JSON. Loads() function: convert the JSON format data into a dictionary. When there is null, it is converted to none

import json

ret = '{"createdAt":"","updatedAt":"", "dataSets":null}'
ret = json.loads(ret)
print(ret)
print(type(ret))

out:
{'createdAt': '', 'updatedAt': '', 'dataSets': None}
<class 'dict'>