How to Solve Python NameError: name ‘json’ is not defined

by | Programming, Python, Tips

This error occurs when you try to use the json module without importing it first. You can solve this error by importing the module using the import keyword. For example,

import json
lst = [1, 2, 3]
json_str = json.dumps(lst)

This tutorial will go through how to solve the error with code examples.


NameError: name ‘json’ is not defined

Python raises the NameError when it cannot recognise a name in our program. In other words, the name we are trying to use is not defined in the local or global scope. A name can be related to a built-in function, module, or something we define in our programs, like a variable or a function.

The error typically arises when:

  • We misspell a name
  • We do not define a variable or function
  • We do not import a module

In this tutorial, the source of the error NameError: name ‘json‘ is not defined is usually due to not importing the module. Let’s look at an example.

Example

JSON stands for JavaScript Object Notation and is a lightweight format for storing and transporting data inspired by the syntax to define JavaScript objects. The module json contains functions for working with JSON data.

The json module is built-in, which means it comes with Python.

Let’s look at an example of using the loads() method to parse a JSON string.

# JSON string:
x =  '{ "name":"Michalis", "age":23, "city":"Athens"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])

The json.loads() method returns a Python dictionary. We can access a value in the dictionary by specifying its key. Let’s try to run the code to get the age value:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [1], in <cell line: 5>()
      2 x =  '{ "name":"Michalis", "age":23, "city":"Athens"}'
      4 # parse x:
----> 5 y = json.loads(x)
      7 # the result is a Python dictionary:
      8 print(y["age"])

NameError: name 'json' is not defined

The error occurred because we did not import the json module. Although json is a built-in module, we still have to import it.

Solution #1: Import json

We can import the module by putting an import statement at the top of the program. Let’s look at the updated code:

import json

# JSON string:
x =  '{ "name":"Michalis", "age":23, "city":"Athens"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])

Let’s run the code to get the age value from the dictionary:

23

Solution #2: Use the from keyword

We can also use the from keyword to import a specific variable, class or function from a module. In this case, we want to import the loads() method from the json module. Using the from keyword means we do not have to specify the module in the rest of the program, we only need to call the loads() method.

Let’s look at the revised code:

from json import loads

# JSON string:
x =  '{ "name":"Michalis", "age":23, "city":"Athens"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])

Let’s run the code to get the age value from the dictionary:

23

Summary

Congratulations on reading to the end of this tutorial!

For further reading on NameErrors, go to the article: How to Solve Python NameError: name ‘os’ is not defined

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!