-2

I have a json dictionary (result_dict_tag) as follows:

{'10644906845': {'photo': {'id': '10644906845',
   'tags': {'tag': [{'_content': 'srilanka',
      'author': '34665545@N05',
      'authorname': 'CrishyM',
      'id': '34660205-10644906845-23098',
      'machine_tag': 0,
      'raw': 'sri lanka'},
     {'_content': 'navy',
      'author': '34665545@N05',
      'authorname': 'CrishyM',
      'id': '34660205-10644906845-7137',
      'machine_tag': 0,
      'raw': 'navy'},
     {'_content': 'vessel',
      'author': '34665545@N05',
      'authorname': 'CrishyM',
      'id': '34660205-10644906845-8792',
      'machine_tag': 0,
      'raw': 'vessel'}]}},
  'stat': 'ok'},
      '11778819726': {'photo': {'id': '11778819726',
   'tags': {'tag': [{'_content': 'sri',
      'author': '44455081@N07',
      'authorname': 'Vinchel',
      'id': '44433751-11778819726-12165',
      'machine_tag': 0,
      'raw': 'sri'},
     {'_content': 'lanka',
      'author': '44455081@N07',
      'authorname': 'Vinchel',
      'id': '44433751-11778819726-12166',
      'machine_tag': 0,
      'raw': 'lanka'},
     {'_content': 'mirissa',
      'author': '44455081@N07',
      'authorname': 'Vinchel',
      'id': '44433751-11778819726-1158107',
      'machine_tag': 0,
      'raw': 'mirissa'}]}},
  'stat': 'ok'}}

What I want is to extract the 'raw' words from each dictionary (e.g.result_dict_tag['10644906845']['photo']["tags"]['tag'][0]['raw']) and and create a dataframe consisting user id and relevant raw words for each user. I feel this need some for loop. I have written one and it only results the 'raw' words related to first user only. I have gone through documentation and various questions similar to this. But couldn't figure it out. Any help is appreciated.

Tharindu
  • 311
  • 1
  • 2
  • 11
  • You may find this question helpful https://stackoverflow.com/questions/47651708/iteration-over-the-dictionary-and-extracting-values/47651828#47651828 – Sam Dec 08 '17 at 12:21

2 Answers2

0

You can extract these in a single list comprehension

raw_words = [mydict[i]['photo']["tags"]['tag'][k]['raw']
             for k in range(len(mydict[i]['photo']["tags"]['tag']))
             for i in mydict]

print(raw_words)
>> ['sri', 'sri lanka', 'lanka', 'navy', 'mirissa', 'vessel']
Varun Balupuri
  • 363
  • 5
  • 17
  • Thanks, @Varun, I have tried it. But it showed an error as name i is not defined. I can't figure it out as I am a bit new to python. Any help is appreciated. – Tharindu Dec 08 '17 at 13:04
  • Make sure the list comprehension is properly formatted as a single line. – Varun Balupuri Dec 08 '17 at 13:06
0

If I have understood you correctly, what you want is a list of dicts with each dict containing an id and a row.

Add these lines of code below your dict:

new_list = result_dict_tag['10644906845']['photo']["tags"]['tag']

new_desired_list = []

for index in range(len(new_list)):

  tempDict = {} 

  for key in new_list[index]:

      if(key=='id'):
          tempDict['id'] = new_list[index][key]

      if(key=='raw'):
          tempDict['row'] = new_list[index][key]

  new_desired_list.append(tempDict)

print(new_desired_list)
waqasgard
  • 801
  • 7
  • 25