Home > database >  Dict Update method only adds last value
Dict Update method only adds last value

Time:01-20

I have the following code (link here)

I have looked at similar posts and examples online but have not been able to understand / resolve the issue on why .update only adds the last value to the dictionary

import json

def main():
    jsonData = {'CompanyId': '320193', 'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': 
        [{'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2020-09-26'}, 'value': '39789000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2019-09-28'}, 'value': '50224000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2018-09-29'}, 'value': '25913000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2021-09-25'}, 'value': '35929000000'}], 
        
        'NetIncomeLoss': [{'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2020-09-27', 'endDate': '2021-09-25'}, 'value': '94680000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2019-09-29', 'endDate': '2020-09-26'}, 'value': '57411000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2018-09-30', 'endDate': '2019-09-28'}, 'value': '55256000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2020-09-27', 'endDate': '2021-09-25'}, 'segment': 
        {'dimension': 'us-gaap:StatementEquityComponentsAxis', 'value': 'us-gaap:RetainedEarningsMember'}, 'value': '94680000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2019-09-29', 'endDate': '2020-09-26'}, 
        'segment': {'dimension': 'us-gaap:StatementEquityComponentsAxis', 'value': 'us-gaap:RetainedEarningsMember'}, 'value': '57411000000'}, 
        {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2018-09-30', 'endDate': '2019-09-28'}, 
        'segment': {'dimension': 'us-gaap:StatementEquityComponentsAxis', 'value': 'us-gaap:RetainedEarningsMember'}, 
        'value': '55256000000'}]}
        
    jsonDump = json.dumps(jsonData)
    actualJson = json.loads(jsonDump)
    finalJson = fixData(actualJson)
    print(finalJson)

def fixData(jsonData):
    jsonDump = json.dumps(jsonData)
    actualJson = json.loads(jsonDump)
    finalObject = {}
    finalObject['CompanyId'] = actualJson.get("CompanyId")
    CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = actualJson.get(
        "CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents")
    one = dataRepeat(CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents,"CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents")
    finalObject.update(one)
    NetIncomeLoss = actualJson.get("NetIncomeLoss")
    two = dataRepeat(NetIncomeLoss, "NetIncomeLoss")
    finalObject.update(two)
    return finalObject

def dataRepeat(item, property):
    final = {}
    test = {}
    mList = []
    super_dict = {}
    for i in item:
        decimals = i.get("decimals")
        unitRef = i.get("unitRef")
        if (i.get("period").get("startDate")):
            startDate = i.get("period").get("startDate")
        else:
            startDate = None
        if (i.get("period").get("endDate")):
            endDate = i.get("period").get("endDate")
        else:
            endDate = None
        if (i.get("period").get("instant")):
            instant = i.get("period").get("instant")
        else:
            instant = None
        propertyValue = i.get("value")
        
        final['Decimals'] = decimals
        final['UnitRef'] = unitRef
        final['StartDate'] = startDate
        final['EndDate'] = endDate
        final['Instant'] = instant
        final[f"{property}"] = propertyValue
        
        mList.append({"Decimals": final['Decimals']}),
        mList.append({"UnitRef": final['UnitRef']}),
        mList.append({"StartDate": final['StartDate']}), 
        mList.append({"EndDate": final['EndDate']}), 
        mList.append({"Instant": final['Instant']}), 
        mList.append({f"{property}": final[f"{property}"]})
     
#    ou = {}
#    for d in mList:
#        for key, value in d.items():
#            ou.setdefault(key, []).append(value)
#    return ou
    ou = {}
    for d in mList:
        ou.update(d)
    return ou

main()

What I get :

{'CompanyId': '320193', 'Decimals': '-6', 'UnitRef': 'usd', 'StartDate': '2018-09-30', 'EndDate': '2019-09-28', 'Instant': None, 'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '35929000000', 'NetIncomeLoss': '55256000000'}

vs Entire Data:

{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': None}
{'EndDate': None}
{'Instant': '2020-09-26'}
{'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '39789000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': None}
{'EndDate': None}
{'Instant': '2019-09-28'}
{'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '50224000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': None}
{'EndDate': None}
{'Instant': '2018-09-29'}
{'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '25913000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': None}
{'EndDate': None}
{'Instant': '2021-09-25'}
{'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '35929000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': '2020-09-27'}
{'EndDate': '2021-09-25'}
{'Instant': None}
{'NetIncomeLoss': '94680000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': '2019-09-29'}
{'EndDate': '2020-09-26'}
{'Instant': None}
{'NetIncomeLoss': '57411000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': '2018-09-30'}
{'EndDate': '2019-09-28'}
{'Instant': None}
{'NetIncomeLoss': '55256000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': '2020-09-27'}
{'EndDate': '2021-09-25'}
{'Instant': None}
{'NetIncomeLoss': '94680000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': '2019-09-29'}
{'EndDate': '2020-09-26'}
{'Instant': None}
{'NetIncomeLoss': '57411000000'}
{'Decimals': '-6'}
{'UnitRef': 'usd'}
{'StartDate': '2018-09-30'}
{'EndDate': '2019-09-28'}
{'Instant': None}
{'NetIncomeLoss': '55256000000'}

Expected output would be where the finalJson contains all the data

CodePudding user response:

You have the lot of inefficiences in code that is detracting from an issue at hand.

Ex:

jsonDump = json.dumps(jsonData)
    actualJson = json.loads(jsonDump)

What is the point? To equal just as:

actualJson = jsonData

Or even:

actualJson = jsonData.copy()

Next:

finalObject = {}
    finalObject['CompanyId'] = actualJson.get("CompanyId") 

This can be to:

finalObject = {'CompanyId' :  actualJson.get("CompanyId") }

Then:

if (i.get("period").get("instant")):

But i.get if not there, is None, so I think for in case your error, then don't handle as such:

if (i["period"].get("instant")):

And then also:

f"{property}"

Specifically, here:

mList.append({f"{property}": final[f"{property}"]})

But what is property? I think it's just a string:

property

So:

mList.append({property: final[property]})

CodePudding user response:

What I see from your data:

We know that a dictionary does not have duplicate keys:

I want to show with an example:

d={}

list_of_dict=[{1:'a',2:'b'},{1:'c'}]

Now, when you apply update:

for x in list_of_dict:
  d.update(x)

Now, you will get:

#{1: 'c', 2: 'b'}

You see update() will override the the value of key 1 from 'a' to 'c'.

This is what exactly happening in your code

  • Related