-2

I have this JSON file, which I need for geolocation:

Here's what it looks like:

{
"status": "success",
"country": "COUNTRY",
"countryCode": "COUNTRY CODE",
"region": "REGION CODE",
"regionName": "REGION NAME",
"city": "CITY",
"zip": "ZIP CODE",
"lat": LATITUDE,
"lon": LONGITUDE,
"timezone": "TIME ZONE",
"isp": "ISP NAME",
"org": "ORGANIZATION NAME",
"as": "AS NUMBER / NAME",
"query": "IP ADDRESS USED FOR QUERY"

}

Actually, here's what it looks like when I send a GET request:

{"as":"AS7922 Comcast Cable Communications, Inc.","city":"Baltimore","country":"United States","countryCode":"US","isp":"Comcast Cable","lat":39.3281,"lon":-76.6385,"org":"Comcast Cable","query":"69.138.1.254","region":"MD","regionName":"Maryland","status":"success","timezone":"America/New_York","zip":"21211"}

How can I parse this data in Python? To output and print.

Thanks! (Sorry if this may be a duplicate, I can't find anything on here that helps me out)

Arman Shah
  • 111
  • 1
  • 6

1 Answers1

0

You can make this easily in Python with module json. Look the example:

#!/usr/bin/env python3

import json


response = {"as":"AS7922 Comcast Cable Communications,    Inc.","city":"Baltimore","country":"United States","countryCode":"US","isp":"Comcast Cable","lat":39.3281,"lon":-76.6385,"org":"Comcast Cable","query":"69.138.1.254","region":"MD","regionName":"Maryland","status":"success","timezone":"America/New_York","zip":"21211"}

data_str = json.dumps(response)  # serialize object in JSON format string

data = json.loads(data_str)   #  deserialize JSON string to Python object

print ('{} \n'.format(data_str))
print ('{} \n'.format(data))


# show all itens
for key in response:
    print ('{} -> {}'.format(key, response[key]))

print ('#' * 100)

for key in data:
    print ('{} -> {}'.format(key, data[key]))