Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python/Django json.loads() error when loading JSON File

In my Django project I have the following directory structure:

project/build/contracts/MyFile.json

And I am writing code in the following directory

project/homeapp/views.py

In my views.py I have the following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

with open("../build/contracts/MyFile.json", "r") as f:
    data = json.loads(f.read())
    abi = data["abi"]

When I try to python manage.py runserver I get the following error:
enter image description here

The strange part is that I couldn’t figure out what was wrong so I made a viewstest.py and placed the exact same code in it. When I run it with python .\viewstest.py and print the JSON to console, it works perfectly fine.

I even tried importing the abi variable from viewstest.py to views.py but got the same error. So I assume that it is an error relating to Django, but I can’t seem to figure it out.

Thanks!

>Solution :

It should be json.load() and not json.loads()

Change the following code to:

with open("../build/contracts/MyFile.json", "r") as file:
    data = json.load(file)
    abi = data["abi"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading