0

I have a json file with some data in it.

[
 {
   "@timestamp": "2021-05-07 15:01:19",
   "message": "hello",
   "name": "hi",
   "userID": "001"
 },
 {
   "@timestamp": "2021-05-07 15:01:19",
   "message": "hello",
   "name": "hi",
   "userID": "001"
 }
]

I want to loop through it and output its content but I am getting this error:

AttributeError: 'list' object has no attribute 'values'

my code is this:

import json
with open('final_result.json') as data_file:
    data = json.load(data_file)
    for v in data.values():
        print(v)

can somebody help me to understand why I am getting this error and how to solve it?

thank you much in advance

1 Answer 1

2

The json file consists of 2 json objects. Each json object is equivalent to a dictionary in python. The load function is returning a list of dictionaries. Traverse over the list of dictionaries to get the desired output.

import json

with open('final_result.json') as data_file:
    data = json.load(data_file)
    for v in data:
        for value in v.values():
            print(value)

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.