Kamal
Kamal

Reputation: 630

Unable to load JSON file in python, not sure why?

I have to load a json file into a python script for part of my homework. Below is the part of the code which loads the JSON file:

import json
import GraphImplementation as G
from webbrowser import open
from ParseJson import parseJSONToGraph

def main():
    jsonFile = open("map_data.json")
    jsonData = json.load(jsonFile)
    graph = G.Graph()
    graph = parseJSONToGraph(graph, jsonData)

For some reason, on the json.load(jsonFile) line, the code fails with this error:

$ python main.py
Traceback (most recent call last):
  File "main.py", line 85, in <module>
    main()
  File "main.py", line 13, in main
    jsonData = json.load(jsonFile)
  File "/usr/lib/python2.7/json/__init__.py", line 286, in load
    return loads(fp.read(),
AttributeError: 'bool' object has no attribute 'read'

Does anyone know why this could be? In the interactive python mode I am able to run the load() call without any problem, I am not sure why it wouldnt work when I am executing my script.

Upvotes: 1

Views: 1637

Answers (1)

webbrowser.open does not load a file from URL; instead it opens a web browser with that page, and returns True (a bool value), which passed to json.load causes an exception being thrown.

If you have a file that you want to open, just remove the from webbrowser import open line.

Upvotes: 3

Related Questions