How to Resolve Error "NameError: name 'json' is not defined" in Python
The NameError: name 'json' is not defined is a common Python error that occurs when you try to use JSON functionality without importing the necessary module. Unlike some other languages where standard libraries might be available globally, Python requires you to explicitly import modules before using them.
This guide explains why this error happens and provides the correct way to import and use the json module for serialization (converting objects to strings) and deserialization (converting strings to objects).
Understanding the Error
When you write code like json.loads(...), Python looks for a variable or module named json in the current scope. If it hasn't been defined or imported, Python raises a NameError.
Faulty Code:
data = '{"id": 1, "name": "Alice"}'
# ⛔️ Error: 'json' is not defined because we forgot to import it
user = json.loads(data)
Output:
NameError: name 'json' is not defined
Solution 1: Importing the Entire Module (Standard)
The most common and recommended fix is to import the entire module at the top of your script. This gives you access to all JSON functions (loads, dumps, load, dump) under the json namespace.
# ✅ Correct: Import the module first
import json
data = '{"id": 1, "name": "Alice"}'
user = json.loads(data)
print(user["name"])
Output:
Alice
Solution 2: Importing Specific Functions
If you only need specific functions (like loads or dumps), you can import them directly into your namespace. This allows you to call them without the json. prefix.
# ✅ Correct: Import specific functions
from json import loads, dumps
data = {"id": 1, "name": "Bob"}
# Use dumps() directly
json_string = dumps(data)
print(json_string)
# Use loads() directly
user = loads(json_string)
print(user["name"])
Output:
{"id": 1, "name": "Bob"}
Bob
Solution 3: Using an Alias
In scenarios where json might conflict with a variable name (though rare for this module) or if you prefer brevity, you can alias the module.
import json as j
data = {"id": 3, "name": "Charlie"}
# Use the alias 'j'
json_string = j.dumps(data)
print(json_string)
Output:
{"id": 3, "name": "Charlie"}
Practical Examples: Loading and Saving Data
Once the module is imported correctly, here are the four core operations you will perform.
Serializing (Python -> JSON String)
import json
data = {"key": "value"}
json_str = json.dumps(data)
print(data)
Deserializing (JSON String -> Python)
import json
json_str = '{"key": "value"}'
data = json.loads(json_str)
Writing to a File
import json
data = {"log": "system start"}
with open("log.json", "w") as f:
json.dump(data, f)
Reading from a File
import json
with open("log.json", "r") as f:
data = json.load(f)
Conclusion
To resolve NameError: name 'json' is not defined:
- Add
import jsonat the top of your script. - Verify Spelling: Ensure you typed
import json(lowercase). - Check Scope: Ensure the import is not inside a function that hasn't been called yet (though top-level imports are standard practice).