Nameerror name ‘json’ is not defined

In this article, we’ll walk you through how to fix the nameerror name ‘json’ is not defined error message in Python.

If you’re having a hard time dealing with this error, we got you.

Apparently, because this article discusses the solutions for nameerror: name ‘json’ is not defined.

What is “nameerror name ‘json’ is not defined”?

The nameerror: name ‘json’ is not defined occurs when you’re trying to use the json module but the module is not yet imported or installed.

This error message can occur due to several reasons, such as a typo in the module name or a missing import statement at the beginning of the code.

For example:


data = {"website": "Itsourcecode", "visits": 1000000}
json_string = json.dumps(data)

print(json_string)

If you try to run this code, it will throw a nameerror that indicates name ‘json’ is not defined.

How to fix “nameerror: name ‘json’ is not defined”?

To fix the error message nameerror name ‘json’ is not defined, you need to add the line import json at the top or beginning of your Python script.

1. Check if the json module is installed

You have to check if the json module is already installed, if yes, you can use and import it. However, if it’s not, you need to install it first.

Use the following command to install json module:

pip install simplejson

If the above command did not install the module, use this command:

✅ easy_install simplejson

Oftentimes, easy_install works if pip doesn’t function well.

2. Import the json module

The most common solution to this error is to import the json module at the beginning of your code.

Take note: you have to import the module at the top or beginning of your code so that you can able to use it throughout your Python code.

For example:

import json

data = {"website": "Itsourcecode", "visits": 1000000}
json_string = json.dumps(data)

print(json_string)

As you can see, the “json” module is imported at the top or beginning of the code, which allows us to use the “dumps()” function to convert the dictionary to JSON.

Output:

{"website": "Itsourcecode", "visits": 1000000}

3. Use “from” keyword to import specific

You can also use the “from” keyword to import specific functions from the “json” module.

✅ from json import dumps

data = {"website": "Itsourcecode", "visits": 1000000}
json_string = dumps(data)

print(json_string)

As you will notice its results in the same output in our previous example above, but we have imported only the “dumps()” function from the “json” module using the “from” keyword.

Output:

{"website": "Itsourcecode", "visits": 1000000}

Conclusion

The nameerror: name ‘json’ is not defined occurs when you’re trying to use the json module but the module is not yet imported or installed.

This article explores what this error is all about and already provides solutions to help you fix this error.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊