1

I have a list of dictionaries in this format:

user_input =[{
    'user':'guest',
    'record':
    '{"message":"User Request","context":{"session_id":"YWOZe"},"level":100}'
},
{
    'user':'guest',
    'record':
    '{"message":"User Inquiry","context":{"session_id":"YWOZf"},"level":100}'
},
]

What I want is, I want to use eval in eval(user_input[0]['record']) and eval(user_input[1]['record']) by iterating over two dictionaries, so far I could do for one dictionary as

def record_to_objects(data:dict):
    new_dict = {}
    for key, value in data.items():
        if data[key] == data['record']:
            new_dict['record'] = eval(data['record'])
            r = data
            del r['record']
            return {**r, **new_dict} 

But for two dictionaries in a list, I got only one result(as the last item) using "double for loop", maybe I am not returning in a proper way. Expected output:

output = [
    
    {'user': 'guest',
     'record': {'message': 'User Request',
                'context': {'session_id': 'YWOZe'},
                'level': 100}},
    
    {'user': 'guest',
     'record': {'message': 'User Inquiry',
                'context': {'session_id': 'YWOZf'},
                'level': 100}}
]

Can somebody help me? I want a list of dictionaries and both should have been processed by the eval method.

  • 1
    I think it would be clearer if you can please edit the question to show what output you are hoping to get in this example. Most likely, a good solution will not involve the use of `eval`. – alani May 24 '22 at 22:12
  • If i understand well you want to replace the string representing dict into the evaluated dict (object)? – Fredericka May 24 '22 at 22:15

1 Answers1

3

That looks like JSON. You should not use eval() for this. Instead, use json.loads() with a for loop:

import json

for item in user_input:
    item["record"] = json.loads(item["record"])

This outputs:

[
 {
  'user': 'guest',
  'record': {'message': 'User Request', 'context': {'session_id': 'YWOZe'}, 'level': 100}
 },
 {
  'user': 'guest',
  'record': {'message': 'User Inquiry', 'context': {'session_id': 'YWOZf'}, 'level': 100}
 }
]
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • I want the remaining data as well not only item['record'], how can I get remaining data and merged with these outputs for each dictionaries. – Jayshwor Khadka May 24 '22 at 22:32
  • See the edit to my post. – BrokenBenchmark May 24 '22 at 22:36
  • Thanks, nearly got the result, but still I want in a list of dictionaries, this gave me one whole dictionary. `{'user': 'guest', 'record': {'message': 'User Request', 'context': {'session_id': 'YWOZe'}, 'level': 100}}, {'user': 'guest', 'record': {'message': 'User Inquiry', 'context': {'session_id': 'YWOZf'}, 'level': 100}}` while I want a list of dictionaries so that I can call out[0] and so on. – Jayshwor Khadka May 24 '22 at 22:50
  • 1
    @JayshworKhadka That's a list of dictionaries without the leading and trailing bracket. – BrokenBenchmark May 24 '22 at 23:01