how to fix nameerror name json is not defined in python

How to Fix – NameError: name ‘json’ is not defined

If you are a Python developer, you might have encountered the error message “NameError: name ‘json’ is not defined” at some point in your coding journey. This error occurs when you try to use the json module in your code, but Python cannot find it.

how to fix nameerror name json is not defined in python

In this tutorial, we will discuss the common causes of this error and how to fix it.

Why does the NameError: name 'json' is not defined error occur?

This error occurs when you try to use the json module in your Python code, but Python cannot find the json module in its namespace. The following are some of the scenarios in which this error usually occurs.

  1. You have not imported the json module.
  2. You have imported the json module using a different name.

How to fix the NameError: name 'json' is not defined?

The json module in Python is a built-in module that allows you to encode and decode JSON (JavaScript Object Notation) data. JSON is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. The json module provides the following two useful methods: json.dumps() to encode Python objects into a JSON formatted string, and json.loads() to decode a JSON formatted string into a Python object.

Since this module is part of the Python Standard Library, you don’t need to separately install it. You can simply import it and start using it.

Let’s now look at the above scenarios that may result in the above error in detail.

The json module is not imported

It can happen that you are trying to use the json module without even importing it. This is because Python does not recognize the json library and its functions until it is imported into the code.

For example, let’s try to use the json module without importing it and see what we get.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# note that the json module is not imported

# create a dictionary
person = {
    "name": "Kundan",
    "age": 26,
    "city": "Varanasi"
}

# convert dictionary to JSON string
person_json = json.dumps(person)

# print JSON string
print(person_json)

Output:

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Cell In[1], line 11
      4 person = {
      5     "name": "Kundan",
      6     "age": 26,
      7     "city": "Varanasi"
      8 }
     10 # convert dictionary to JSON string
---> 11 person_json = json.dumps(person)
     13 # print JSON string
     14 print(person_json)

NameError: name 'json' is not defined

We get a NameError stating that the name json is not defined. To use the json library, you need to import it first.

import json

# create a dictionary
person = {
    "name": "Kundan",
    "age": 26,
    "city": "Varanasi"
}

# convert dictionary to JSON string
person_json = json.dumps(person)

# display JSON string
person_json

Output:

'{"name": "Kundan", "age": 26, "city": "Varanasi"}'

Here, we are importing the json module first and then using it to convert a Python dictionary object into a json string. You can see that we did not get any errors here.

The json module is imported using a different name

If you import the json module using a different name, for example import json as jsn, and then try to use the name “json” to use it, you will get a NameError because the name “json” is not defined in your current namespace.

Let’s look at an example.

import json as jsn

# create a dictionary
person = {
    "name": "Kundan",
    "age": 26,
    "city": "Varanasi"
}

# convert dictionary to JSON string
person_json = json.dumps(person)

# display JSON string
person_json

Output:

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Cell In[1], line 11
      4 person = {
      5     "name": "Kundan",
      6     "age": 26,
      7     "city": "Varanasi"
      8 }
     10 # convert dictionary to JSON string
---> 11 person_json = json.dumps(person)
     13 # display JSON string
     14 person_json

NameError: name 'json' is not defined

We get a NameError: name 'json' is not defined. This is because we have imported the josn module with the name json but we’re trying to use it using the name json.

To fix this error, you can either access json using the name that you have used in the import statement or import json without an alias. Note that generally, the convention is to import the json module without any aliases.

Conclusion

In conclusion, encountering a NameError: name 'json' is not defined error can be frustrating, but it is a common issue that can be easily fixed. By following the steps outlined in this tutorial, you should now have a better understanding of what causes this error and how to resolve it. Remember to always check your code for typos and syntax errors, and to import any necessary modules before using them in your code. With these tips in mind, you should be able to tackle any NameError errors that come your way.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top