Fix - NameError: name 'null' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

Fix - NameError: name 'null' is not defined in Python #

The Python "NameError: name 'null' is not defined" occurs when we use null instead of None in Python or we forget to parse JSON data into native Python objects. To solve the error, replace any occurrences of null with None in your code.

nameerror name null is not defined

Here is an example of how the error occurs.

main.py
# ⛔️ NameError: name 'null' is not defined val = null print(val)

To solve the error, we have to use None instead of null in Python.

main.py
val = None print(val) # 👉️ None

The None object is used to represent the absence of a value.

Make sure to replace all occurrences of null in your code with None.

If you have a JSON string, you have to parse it to a native Python object before accessing specific properties.

main.py
import json json_str = json.dumps({'name': None}) print(json_str) # 👉️ '{"name": null}' print(type(json_str)) # 👉️ <class 'str'> # ✅ Parse JSON string to native Python object native_python_obj = json.loads(json_str) print(native_python_obj) # 👉️ {'name': None} print(type(native_python_obj)) # 👉️ <class 'dict'> print(native_python_obj['name']) # 👉️ None

We used the json.dumps method to serialize a Python object to a JSON formatted string.

Notice that None becomes null when converted to a JSON string.

Conversely, you can use the json.loads method to deserialize a JSON string to a native Python object.

When we parse a JSON string into a Python object, null values become None.

If you have a JSON string in your code, you can use the json.loads() method to convert it to a Python object.

main.py
import json json_str = r'''{"name": null, "age": null}''' native_python_obj = json.loads(json_str) print(native_python_obj) # 👉️ {'name': None, 'age': None}

Notice that all null values become None after parsing the JSON string into native Python.

Alternatively, you can declare a null variable and assign it a value of None.

main.py
null = None my_dict = {"name": null, "age": null} print(my_dict)

However, note that this is a hacky solution and might be confusing to readers of your code.

If you use the requests module to make HTTP requests, you can call the json() method on the response object to parse the JSON string into a native Python object.

main.py
import requests def make_request(): res = requests.get('https://reqres.in/api/users') print(type(res)) # 👉️ <class 'requests.models.Response'> # ✅ Parse JSON to native Python object parsed = res.json() print(parsed) print(type(parsed)) # 👉️ <class 'dict'> make_request()

The res variable is a Response object that allows us to access information from the HTTP response.

We can call the json() method on the Response object to parse the JSON string into a native Python object which would convert any null values to their Python equivalent of None.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.