How to format JSON text in Python?

When I call the JSON file for Vikent van Gogh List of wikipedia page works using this url, it obviously returns huge text, which I think is a kind of list dictionary.

Now someone has already shown me the Python Wikipedia import function, so skip this. How can I decrypt this JSON? I feel like I tried everything in the Python 3 library and always get an error, for example, I get if I try to execute this code, for example:

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

he 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 does not return an error, but simply does not look

>>>

>>>

But if I select this empty space and paste it in, it inserts the same text.

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

If I try the for loop:

for entry in json_data:
    print(entry)

He returns

>>> 
query
warnings
>>> 

. , , , ? JSON Python ? , , , ?

+4
1

JSON Python?

json_data = json.loads(json_string)

:

for entry in json_data:
    print(entry)

keys . , :

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

, , keys. , , dict:

{u'query': {...}, u'warnings': {...}}
+2

Source: https://habr.com/ru/post/1542761/


All Articles