1

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.

0

1 Answer 1

1

Your json is a array of objects. So your python will be a list of dictionaries. You need to do something like obj[0].get('user_agent').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.