0

I am getting data in json and when i am trying to get value of one key from that json it is not working for me.

   print(type(file_data))
   time_file1 = json.loads(file_data)
   print(time_file1)
   print(type(time_file1))
   print(time_file1['time_created'])

Output and error for this i am getting is:-

[{'archival_state': None, 'etag': None, 'md5': None, 'name': '.passwd.json', 'size': 1447, 'storage_tier': None, 'time_created': '2022-07-22T07:09:47.342000+00:00', 'time_modified': None}]
<class 'list'>
Traceback (most recent call last):
  File "/opt/faops/spe/ocifabackup/lib/python/common/opc.py", line 69, in <module>
    update_password_json_oss()
  File "/opt/faops/spe/ocifabackup/lib/python/common/opc.py", line 46, in update_password_json_oss
    print(time_file1['time_created'])
TypeError: list indices must be integers or slices, not str

Here when i am getting dat its str type and when i am doing json loads its changing to list type and in both cases i am not able to fetch value based on key.

1 Answer 1

1

As your time_file1 is in list of dictionary please try below line to fetch dictionary with key.

time_file1[0]['time_created']
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.