I hava a json file called json.json which has the following data:
[
{
"browser": "Mozilla",
"browser_version": "5.0 (X11)",
"operating_system": "Linux x86_64",
"user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0",
"cookie": "",
"java_enabled": "function javaEnabled() { [native code]}",
"pages_viewed": "3",
"color_depth": "24",
"screen_resolution": "1920x1080"
},
{
"browser": "Mozilla",
"browser_version": "5.0 (X11)",
"operating_system": "Linux x86_64",
"user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0",
"cookie": "",
"java_enabled": "function javaEnabled() { [native code]}",
"pages_viewed": "3",
"color_depth": "24",
"screen_resolution": "1920x1080"
}
]
My python code:
import json
#open and read file
json_file = open('json.json','r')
json_data = json_file.read()
#parse json data
obj = json.loads(json_data)
#print json data
print(str(obj['browser'])) #except to print: Mozilla
print(str(obj['browser_version'])) #except to print: 5.0 (X11)
print(str(obj['operating_system'])) #and so on...
print(str(obj['user_agent']))
print(str(obj['cookie']))
print(str(obj['java_enabled']))
print(str(obj['pages_viewed']))
print(str(obj['color_depth']))
print(str(obj['screen_resolution']))
I am trying to parse the data with python, however when I run the python code, I get the following error:
print(str(obj['browser']))
TypeError: list indices must be integers or slices, not str
My goal is to ONLY print the values of each JSON key.