-1

I am loading objects from json file and putting it in a list. I want to access the value of "details". I have tried something like this -

    item_list = [{
    "text": "some text here",
    "timestamp": "Tue Jun 30 16:46:48 +0000 2020",
    "details": "{'id': '1278007086765522944', 'hash': ['e66d35b329a7c2cff66075eaf4530d13']}"}]

for d in item_list.iterkeys():
        print(d['item'])

but I am getting UndefinedError: 'unicode object' has no attribute 'iterkeys'

hotshot_02
  • 103
  • 2
  • 7
  • you don't need to add ' around '{' and '}', just {'item1':'value1','item2':'value2'} will be fine – Kristaps Jul 15 '20 at 10:16
  • 1- check the comment by @Kristaps , 2- the `iterkeys()` does not work on the list. – vvk24 Jul 15 '20 at 10:20
  • @Kristaps this the json file from a job i am getting and i have to process the same. I have to take each json object and put it in a list and then process it. – hotshot_02 Jul 15 '20 at 10:58
  • @Kristaps thanks, i have edited the json in my question. – hotshot_02 Jul 16 '20 at 05:23
  • @Hotshot02 no problem :) (oooh, so you're getting part of the json as a string) - iterkeys is a python 2 (?) way of getting the keys from a dictionary - your outer structure is a list (it's wrapped in [ ] instead of being wrapped in { } - if you're looking to loop through the entries and access details you'll want to just do > > > for d in item_list: print(d["details"]) < < < you can take a look at https://medium.com/@paulrohan/python-list-vs-tuple-vs-dictionary-4a48655c7934 ) – Kristaps Jul 16 '20 at 06:47

1 Answers1

0

Be careful that item_list is a list not a dictionary and iterkeys() only works on dictionaries.

Anyway, one solution is to perform the following steps:

  • take each element in item_list (the elements would be dictionaries)
  • get the list of values of each dictionary from above
  • check the type of the value and if it is a dictionary itself, then extract the values of that dictionary and process them in any way you need
for dict_item in item_list:
    for value in dict_item.values():
        if type(value) == dict:
            for target_value in value.values():
                # do whatever you need with the target_value
                print(target_value)

Make sure you also properly format your item_list (see @Kristaps comment).

[Section added after the question was updated]

I got it. So, somehow you get the dictionary embedded in a string. In this case first thing you need to do is to convert that string to a dictionary. There are a few approaches for this. You can find interesting examples here:

I prepared a little example using ast:

import ast 

for dict_item in item_list:
    for value in dict_item.values():
        try:
            # try to evaluate the value; the evaluation might raise some exception
            res = ast.literal_eval(value)
            if type(res) == dict:
                # dictionary found; do whatever you need with it
                print(res)
        except SyntaxError:
            # just continue to the next value
            continue
Ciprian Stoica
  • 2,309
  • 5
  • 22
  • 36
  • thanks, i have edited the json but i am still not able to get the "details" dict. – hotshot_02 Jul 16 '20 at 05:24
  • @Hotshot02, see my updated answer. Btw, I suggest you always specify the Python version that you are using. Some functionalities only work on Python 3 and don't work in Python 2 or the other way around. There might also be differences between minor versions of Python. So, it would be useful to specify your exact version. – Ciprian Stoica Jul 16 '20 at 08:10