If you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
Answer from Lior on Stack OverflowGetting values from JSON using Python - Stack Overflow
How to get JSON from webpage into Python script - Stack Overflow
How to get value / content in JSON object with python - Stack Overflow
List all JSON keys in a file to identify database column names from a file using Python
Videos
If you want to iterate over both keys and values of the dictionary, do this:
for key, value in data.items():
print(key, value)
What error is it giving you?
If you do exactly this:
data = json.loads('{"lat":444, "lon":555}')
Then:
data['lat']
SHOULD NOT give you any error at all.
Get data from the URL and then call json.loads e.g.
Python3 example:
import urllib.request, json
with urllib.request.urlopen("http://maps.googleapis.com/maps/api/geocode/json?address=google") as url:
data = json.loads(url.read().decode())
print(data)
Python2 example:
import urllib, json
url = "http://maps.googleapis.com/maps/api/geocode/json?address=google"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data
The output would result in something like this:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Charleston and Huff",
"short_name" : "Charleston and Huff",
"types" : [ "establishment", "point_of_interest" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
...
I'll take a guess that you actually want to get data from the URL:
jsonurl = urlopen(url)
text = json.loads(jsonurl.read()) # <-- read from it
Or, check out JSON decoder in the requests library.
import requests
r = requests.get('someurl')
print r.json() # if response type was set to JSON, then you'll automatically have a JSON response here...
Use json.loads it will convert the json string to a dict containing dicts, list, etc.
Edit 2:
You can access each item like this: json_object['scans']['TotalDefense']['version']
contd.
But you may also need to json.loads(json.load(response)) as I mentioned in my comment below.
Edit 1:
In your example, we should not be seeing "scans" again for print json_object['scans'].
From http://docs.python.org/library/json.html:
>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
That page also has info on how to do more complex decoding.
import json
import urllib
import urllib2
url = "https://www.virustotal.com/vtapi/v2/file/report"
parameters = {"resource": "2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1",
"apikey": "12312312312312312"}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
json_object = response.read()
print '\n '
response_dict = json.loads(json_object)
print response_dict.get("response_code",{})
This outputs the response_code's value in the object. To go with nested values, a response_dict.get("scans",{}).get("AVG",{})
could be done. THANK YOU EVERYONE!
I am learning Python, and in particular, working with JSON and sqlite in Python. Ultimately I plan to use Python to load the JSON into a sqlite database.
Here is the question: Is there a way in to list all of the keys from a JSON file (not from a string) using Python? I want a list of all of the keys so I can determine what columns I will need/use in my sqlite table(s), without having to manually read the file and make a list.
BTW, this is something along the lines of using INFORMATION_SCHEMA.COLUMNS in SQL Server, or the FINDALL in Python for XML.
All of this is for personal learning, so I'm not looking to use other technologies, I'm sticking to Python, JSON, and sqlite on purpose.