Thanks for this. When I run the script with
Is there something else I need to add?
======= EDIT =======
I needed to add the indexing piece (not sure correct terminology), but it is the [0] part in the below code.
resp.json()I get no output, but when I run it with
print (json.dumps(resp.json(), indent=2))I get the full output. When I try to add in specific libraries (such as "hosts_file") I get the error
Error:resp.json()["hosts_file"]
.Is there something else I need to add?
======= EDIT =======
I needed to add the indexing piece (not sure correct terminology), but it is the [0] part in the below code.
print (resp.json()[0]['id'])
(Mar-25-2022, 09:41 PM)snippsat Wrote:deanhystad Wrote:resp.json is a json string with all the interesting stuff. You can convert the json string to a Python dictionary to make it easier to work with.deanhystad as info so do Requests have build injson
encoder/decoder added a long time ago(2014).
So the extra step with json string trough json library is not recommend.
can also mess up encoding,resp.json()
directly from Request dos a much better job of figuring out encoding used.
>>> import requests >>> >>> resp = requests.get('https://github.com/timeline.json') >>> resp.json() {'documentation_url': 'https://docs.github.com/v3/activity/events/#list-public-events', 'message': 'Hello there, wayfaring stranger. If you’re reading this then you ' 'probably didn’t see our blog post a couple of years back ' 'announcing that this API would go away: http://git.io/17AROg Fear ' 'not, you should be able to get what you need from the shiny new ' 'Events API instead.'} >>> resp.json()['documentation_url'] 'https://docs.github.com/v3/activity/events/#list-public-events' >>> resp.encoding 'utf-8'