Convert a stringfied list to python list and get values

Viewed 61

I'm quite new to AWS. I have two lambdas, the first one makes a query to RDS and receive a result like [('asdzxc', '12345-0', 123), ('asdzxc', '67890-0', 456)], and the second invokes the first and takes the query. The problem is that the query is received as string: "[('asdzxc', '12345-0', 123), ('asdzxc', '67890-0', 456)]". And I need iterate and take values to use them in another output. But when I iterate, it shows one char per line. I been reading some docs and tried using json.loads(), list(), tuple(), ast.literal_eval. But I still get the same.

Here's a fragment of the first lambda:

query = select_from_bcuc(rut_crypto)
print(query)
str_data = json.dumps(str(query))
return {
        'statusCode': 200,
        'request_id': request_id,
        'body': str_data,
        'msg': 'Hello from extractData!!!'
    }

> output: [('asdzxc', '12345-0', 123), ('asdzxc', '67890-0', 456)]

And the second lambda:

obj = extractData(rut)
    if obj:
        print('Getting data.')
        print(obj['body'])
        p = json.loads(obj['body'])
        print(p)

> output: "[('asdzxc', '12345-0', 123), ('asdzxc', '67890-0', 456)]"
1 Answers

You can use literal_eval:

import ast
s="[('asdzxc', '12345-0', 123), ('asdzxc', '67890-0', 456)]"
a_list = ast.literal_eval(s)
print(type(a_list), a_list)

gives:

<class 'list'> [('asdzxc', '12345-0', 123), ('asdzxc', '67890-0', 456)]
Related