(JSON) NameError: name 'true' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

(JSON) NameError: name 'true' is not defined in Python #

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

Here is an example of how the error occurs.

main.py
# ⛔️ NameError: name 'true' is not defined. Did you mean: 'True'? json_str = {"is_subscribed": true, "is_admin": true}

One way to solve the error is to replace all occurrences of true with True.

main.py
json_str = {"is_subscribed": True, "is_admin": True}
Make sure to replace all occurrences of true in your code with True.

If you are pasting a JSON string into your Python code, wrap it in quotes and mark it as a raw string.

main.py
import json json_str = r'''{"is_subscribed": true, "is_admin": true}''' # 👇️ convert JSON string to native Python object native_python_obj = json.loads(json_str) print(native_python_obj) # 👉️ {'is_subscribed': True, 'is_admin': True}

We used the json.loads method to deserialize a JSON string to a native Python object.

When we parse a JSON string into a Python object, all true values become True.

Alternatively, you can declare a true variable and assign it a value of True.

main.py
true = True my_dict = {"is_subscribed": true, "is_admin": true}

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

You can use the json.dumps method to serialize a Python object to a JSON formatted string.

main.py
import json # ✅ convert Python object to JSON json_str = json.dumps({"is_subscribed": True, "is_admin": True}) print(json_str) # 👉️ {"is_subscribed": true, "is_admin": true} print(type(json_str)) # 👉️ <class 'str'> # ✅ Parse JSON string to Python object native_python_obj = json.loads(json_str) print(native_python_obj) # 👉️ {'is_subscribed': True, 'is_admin': True} print(type(native_python_obj)) # 👉️ <class 'dict'>

Notice that True becomes true when a Python object gets converted to a JSON string.

Conversely, when we parse the JSON string to a native Python object, true becomes True.

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 true values to their Python equivalent of True.

Use the search field on my Home Page to filter through my more than 3,000 articles.