I have a list of dictionaries like this:
[{'accounts': [{'account_number': 'xx6747',
'bank_name': 'Fargo Bank',
'bankid': 85,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20086385.0,
'item_id': 17764259,
'user_id': 18},
{'account_number': '*819',
'bank_name': 'Central Alabama - Bank',
'bankid': 88,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20637127.0,
'item_id': 17761624,
'user_id': 18},
{'account_number': '*056',
'bank_name': 'Central Alabama - Bank',
'bankid': 88,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20087284.0,
'item_id': 17761624,
'user_id': 18},
'created_date_x': Timestamp('2014-03-11 10:48:42'),
'id': 18,
'password': 'NjgrUW9URGZz09',
'status': 1,
'uid': 97560,
'username': 'pepass@yahoo.com'},
{'accounts': [{'account_number': 791,
'bank_name': 'Chase - Bank',
'bankid': 855,
'created_date_y': Timestamp('2015-05-20 08:03:37'),
'item_account_id': 20090104.0,
'item_id': 17764260,
'user_id': 20},
{'account_number': 883,
'bank_name': 'Chase - Bank',
'bankid': 852,
'created_date_y': Timestamp('2015-05-20 08:03:37'),
'item_account_id': 20090105.0,
'item_id': 17764260,
'user_id': 20}],
'created_date_x': Timestamp('2014-03-11 13:42:20'),
'id': 20,
'password': 'bmJlbTN5dFo5QT09',
'status': 1,
'uid': 99408,
'username': 'hor@sanstore.com'}]
It contains other items as well. The created_date_x
and created_date_y
are timestamps. I want to convert this list into a nested dictionary something like this:
{'id': 18,
'password': 'NjgrUW9URGZz09',
'status': 1,
'uid': 97560,
'username': 'pepass@yahoo.com'},
'created_date_x': Timestamp('2014-03-11 10:48:42')
{'accounts':
[{'account_number': 'xx6747',
'bank_name': 'Fargo Bank',
'bankid': 85,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20086385.0,
'item_id': 17764259,
'user_id': 18},
{'account_number': '*819',
'bank_name': 'Central Alabama - Bank',
'bankid': 88,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20637127.0,
'item_id': 17761624,
'user_id': 18},
{'account_number': '*056',
'bank_name': 'Central Alabama - Bank',
'bankid': 88,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20087284.0,
'item_id': 17761624,
'user_id': 18}]}}
After converting it into a dictionary, I want to convert this into a JSON. The problem in converting it into json is that the timestamp
fields give an error as timestamp is not json serializale
. The code I have so far which does not work as I want is:
import pandas as pd
#importing files
df1 = pd.read_excel("C:\\Users\\Desktop\\yod_user.xlsx")
df2 = pd.read_excel("C:\\Users\\Desktop\\yod_bank.xlsx")
df1['created_date'] = df1['created_date'].astype(str)
df2['created_date'] = df2['created_date'].astype(str)
df1.dtypes
#id int64
#username object
#password object
#uid int64
#created_date object
#status int64
#dtype: object
df2.dtypes
#user_id int64
#bankid int64
#account_number object
#item_id int64
#item_account_id float64
#created_date object
#bank_name object
#dtype: object
df_merge = pd.merge(df1, df2, left_on = 'id', right_on ='user_id', how ='inner')
df_merge.dtypes
#id object
#username object
#password object
#uid object
#created_date_x object
#status object
#user_id object
#bankid object
#account_number object
#item_id object
#item_account_id object
#created_date_y object
#bank_name object
#dtype: object
j = df_merge.groupby(['id', 'username', 'password', 'uid',created_date_x', 'status'], as_index=False)\
.apply(lambda x: x[['account_number','user_id','bankid', 'item_id', 'item_account_id','created_date_y', 'bank_name' ]].to_dict('r'))\
.reset_index()\
.rename(columns={0:'accounts'})\
j.dtypes
#id int64
#username object
#password object
#uid int64
#created_date_x object
#status int64
#accounts object
#dtype: object
jdict = j.to_dict('records')
import json
json.dumps(jdict) #This gives an error: TypeError: Object of type 'Timestamp' is not JSON serializable
If the initial list can be converted into a json like a dictionary that will also do. Please Help.