Lets say I have a dictionary like
ship = {'AddNewShipmentV3': {'oShipData': {'ReadyDate': '2021-01-11T12:00:00', 'CloseTime': '2021-01-11T08:12:34', 'ServiceLevel': 'EC', 'ShipperName': 'Test 1/12/2021','SpecialInstructions': 'Invoice - RMA#S, 7800401086-GOOD EQUIP Pickup - DRIVER WILL NEED SHRINK WRAP FOR 2 PALLETS PLEASE CALL', 'Station': 'SLC', 'CustomerNo': '9468', 'BillToAcct': '9468', 'DeclaredType': 'LL'}}}
I want to create a new dictionary (b), by extracting ReadyDate value and store it as ReadyTime and CLoseTime value and store it in CloseDate. and I want to add these new key-value pairs to the starting of the dictionary. I tried this
def ready_date_time(a):
b = {}
b["AddNewShipmentV3"] = {}
b["AddNewShipmentV3"]["oShipData"] = {}
b["AddNewShipmentV3"]["oShipData"]["ReadyTime"] = a["AddNewShipmentV3"]["oShipData"]["ReadyDate"]
if "CloseTime" in a["AddNewShipmentV3"]["oShipData"]:
b["AddNewShipmentV3"]["oShipData"]["CloseDate"] = a["AddNewShipmentV3"]["oShipData"]["CloseTime"]
else:
pass
# this will create a new dictionary (b) with new values "CloseDate" and "ReadyTime" in the starting
of the dictionary
b["AddNewShipmentV3"]["oShipData"].update(a["AddNewShipmentV3"]["oShipData"])
return b
read_date_time(ship)
My output :
{'AddNewShipmentV3': {'oShipData': {'ReadyTime': '2021-01-11T12:00:00', 'CloseDate': '2021-01-11T08:12:34', 'ReadyDate': '2021-01-11T12:00:00', 'CloseTime': '2021-01-11T08:12:34', 'ServiceLevel': 'EC', 'ShipperName': 'Test 1/12/2021', 'SpecialInstructions': 'Invoice - RMA#S, 7800401086-GOOD EQUIP Pickup - DRIVER WILL NEED SHRINK WRAP FOR 2 PALLETS PLEASE CALL', 'Station': 'SLC', 'CustomerNo': '9468', 'BillToAcct': '9468', 'DeclaredType': 'LL'}}}
I want to achieve this task without declaring multiple empty dicts to set inner values. I read it somewhere we can do it using pydash, but am not familiar on using that