How do i pull out certain data from this json response? - python

Here is the link im opening in python:
response = urllib.request.urlopen('http://freegeoip.net/json/1.2.3.4').read()
print(response)
After printing the response it gives the result:
b'{"ip":"1.2.3.4","country_code":"US","country_name":"United States","region_code":"WA","region_name":"Washington","city":"Mukilteo","zip_code":"98275","time_zone":"America/Los_Angeles","latitude":47.913,"longitude":-122.305,"metro_code":819}\n'
My question is, how can i print just the region name? i have this code so far:
import json
import urllib.request
response = urllib.request.urlopen('http://freegeoip.net/json/1.2.3.4').read()
print(response)
result = json.loads(response.decode('utf8'))
Its the last bit of pulling out the specific piece of data im stuck on. Thanks in advance!

Your result object returned from json.loads will be a dictionary, so you can print the value like so:
print result['region_name']
Or even better, a bit more defensively, in the event that key doesn't exist:
print result.get('region_name', 'No region specified')
Also note that your urllib call should be:
response = urllib.urlopen('http://freegeoip.net/json/1.2.3.4').read()

At this point, you'd be able to access it as you would a python object:
result['region_code']

you can use ast.literal_eval:
>>> import ast
>>> my_dict = ast.literal_eval(response.strip())
>>> my_dict['region_name']
'Washington'

Related

How to get value from JSON response using Python [duplicate]

This question already has answers here:
How can I parse (read) and use JSON?
(5 answers)
What are the differences between the urllib, urllib2, urllib3 and requests module?
(11 answers)
Closed last month.
I want to dynamically query Google Maps through the Google Directions API. As an example, this request calculates the route from Chicago, IL to Los Angeles, CA via two waypoints in Joplin, MO and Oklahoma City, OK:
http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
It returns a result in the JSON format.
How can I do this in Python? I want to send such a request, receive the result and parse it.
I recommend using the awesome requests library:
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON Response Content: https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content
The requests Python module takes care of both retrieving JSON data and decoding it, due to its builtin JSON decoder. Here is an example taken from the module's documentation:
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
So there is no use of having to use some separate module for decoding JSON.
requests has built-in .json() method
import requests
requests.get(url).json()
import urllib
import json
url = 'http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false'
result = json.load(urllib.urlopen(url))
Use the requests library, pretty print the results so you can better locate the keys/values you want to extract, and then use nested for loops to parse the data. In the example I extract step by step driving directions.
import json, requests, pprint
url = 'http://maps.googleapis.com/maps/api/directions/json?'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
data = requests.get(url=url, params=params)
binary = data.content
output = json.loads(binary)
# test to see if the request was valid
#print output['status']
# output all of the results
#pprint.pprint(output)
# step-by-step directions
for route in output['routes']:
for leg in route['legs']:
for step in leg['steps']:
print step['html_instructions']
just import requests and use from json() method :
source = requests.get("url").json()
print(source)
OR you can use this :
import json,urllib.request
data = urllib.request.urlopen("url").read()
output = json.loads(data)
print (output)
Try this:
import requests
import json
# Goole Maps API.
link = 'http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false'
# Request data from link as 'str'
data = requests.get(link).text
# convert 'str' to Json
data = json.loads(data)
# Now you can access Json
for i in data['routes'][0]['legs'][0]['steps']:
lattitude = i['start_location']['lat']
longitude = i['start_location']['lng']
print('{}, {}'.format(lattitude, longitude))
Also for pretty Json on console:
json.dumps(response.json(), indent=2)
possible to use dumps with indent. (Please import json)

Get json from url by string indice Flask

Url : 127.0.0.1:5000/mywebservice?&list=hello&list=world&dict={"type":"mail","subject":"Hello","content":"hello world"}
I am trying to get json from the url
list=request.args.getlist(list) allow me to get list like this way list[0] =hello ,list[1] =world
the URL will be like that:
Url : 127.0.0.1:5000/mywebservice&list=hello&list=world&dict[type]=mail&dict[subject]=Hello&dic[content]=hello world
Is there a way that allows me to get the dictionary like this way?
Expected result:
dic=request.args.get(dict)
var= dic['type']
print(var)====> mail
however:
dic = request.args.get(dict)
var = dic['type']
print(var) : Exception
PS: request.json doesn't have access to the url and there is not
dic = request.args.getjson(dict)
If I understood correctly dict from URL will have serialized JSON. You need to use json package to de-serialize it.
import json
url_dict = request.args.get('dict')
dic = json.loads(url_dict)
print (dic['type'])

How to read a JSON result in Python3

I'm trying to calculate the exchange from US Dolar to Brazilian Reais.
I found an REST API from brazilian central bank.
My Python code is receive the API return in JSON format, like that:
{'#odata.context': 'https://was-p.bcnet.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata$metadata#_CotacaoDolarDia(cotacaoVenda)', 'value': [{'cotacaoVenda': 3.8344}]}
In my code I could isolate this part of resulte "[{'cotacaoVenda': 3.8344}]", but I can't isolate only the value "3.8344".
Follow my code:
# Cotação do Dólar V.01
import json
import requests
r = requests.get("https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=#dataCotacao)?#dataCotacao='03-15-2019'&$top=1&$format=json&$select=cotacaoVenda")
if r.status_code == 200:
cotacao = json.loads(r.content)
print(cotacao['value'])
Any idea how can I isolate only the "3.8344" contained in JSON return?
Thank you
The variable cotacao, is a list, which has only one item. So we access it with index [0]. That object, is a dictionary, which we can access its fields using their key:
import json
import requests
r = requests.get("https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=#dataCotacao)?#dataCotacao='03-15-2019'&$top=1&$format=json&$select=cotacaoVenda")
if r.status_code == 200:
cotacao = json.loads(r.content)
print(cotacao['value'][0]['cotacaoVenda'])

Parsing response from Github

How do I make string like "payload={'key':'value'...}" to become a variable payload with value assigned to it "{'key':'value'...}"? Thanks.
Like they said on github, use a query string parser, like this:
import urlparse
str = "payload={'key':'value'}"
print urlparse.parse_qs(str)
Result is this:
{'payload': ["{'key':'value'}"]}
payload = json.loads(jsonstring)
payload = pickle.loads(pickstring)
Don't pickle untrusted data.

Python error when using urllib.open

When I run this:
import urllib
feed = urllib.urlopen("http://www.yahoo.com")
print feed
I get this output in the interactive window (PythonWin):
<addinfourl at 48213968 whose fp = <socket._fileobject object at 0x02E14070>>
I'm expecting to get the source of the above URL. I know this has worked on other computers (like the ones at school) but this is on my laptop and I'm not sure what the problem is here. Also, I don't understand this error at all. What does it mean? Addinfourl? fp? Please help.
Try this:
print feed.read()
See Python docs here.
urllib.urlopen actually returns a file-like object so to retrieve the contents you will need to use:
import urllib
feed = urllib.urlopen("http://www.yahoo.com")
print feed.read()
In python 3.0:
import urllib
import urllib.request
fh = urllib.request.urlopen(url)
html = fh.read().decode("iso-8859-1")
fh.close()
print (html)

Categories