4

When I call the JSON file for Vincent van Gogh's List of Works wikipedia page, using this url, it obviously returns a huge blob of text which I believe is some sort of dictionary of lists.

Now, someone has already shown me Python's import wikipedia feature, so skip that. How can I decode this JSON? I feel like I have tried everything in Python 3's library, and always get an error, like I get if I try this code for example:

data = urllib.request.urlopen(long_json_url)
stuff = json.load(data)   #or json.loads(data)
print(stuff)

it returns

TypeError: the JSON object must be str, not 'bytes'

Or if I try this code:

data = urllib.request.urlopen(longurl)
json_string = data.read().decode('utf-8')
json_data = json.loads(json_string)
print(json_data)

It doesn't return an error, but just what looks like nothing

>>>

>>>

But if I highlight that empty space and paste it, it pastes the same blob of text.

{'warnings': {'main': {'*': "Unrecognized parameter: 'Page'"}}, 'query': {'normalized': [{'from': 'list of works by Vincent van Gogh',... etc

If I try a for loop:

for entry in json_data:
    print(entry)

It returns

>>> 
query
warnings
>>> 

And that's it. So it's not returning an error there, but not really much else, just two values? How would you make the JSON data into a workable Python dict or list? Or at the very least, into a more vertical format that I could actually read?

2
  • 1
    it always returns an error - to save people running and trying your code - please edit what that error is into your post, as well as examples of I have tried several things - what are they - what errors/nothing do they give etc... ? Commented May 31, 2014 at 13:42
  • +1 from me for taking the time to improve your question - very good job :) Commented May 31, 2014 at 14:16

1 Answer 1

2

How would you make the JSON data into a workable Python dict or list?

You're already doing that with

json_data = json.loads(json_string)

This however:

for entry in json_data:
    print(entry)

will only print the keys of your dictionaries. If you want to print the values, you need to use:

for entry in json_data:
    print(json_data[entry])

if you inspect the data, you'll see that there are two keys for the main dictionary. The ones you already got by iterating over the dict:

{u'query': {...}, u'warnings': {...}}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I suspected as much, and that is what I was afraid of. The entire blob of data in this JSON file is one single value that is mapped to one key, a key which is inside a dictionary nested in another dictionary in a dictionary in a dictionary. What on Earth is the use of this wikipedia API? Disappointing. Thank you so much for helping me realize this.
I have begun to sort through the values in this dictionary. I am making progress. Thank you again.
@rami7 You're welcome! Yeah, unfortunately the entire actual content seems to be in some other format. Let me know, if you have any other questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.