user2829222
user2829222

Reputation: 65

Parsing dictionary of dictionaries

I have the following json response:

{'Securities': [{'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Computers & Peripherals', 'SecurityId': 2654, 'DisplayCode': 'AAPL', 'PricingSymbol': 'AAPL US Equity', 'PriceLast': 93.59, 'SecurityDesc': 'APPLE INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 92.04, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Air Freight & Logistics', 'SecurityId': 2660, 'DisplayCode': 'AAWW', 'PricingSymbol': 'AAWW US Equity', 'PriceLast': 38.81, 'SecurityDesc': 'ATLAS AIR WORLDWIDE HOLDINGS', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 38.45, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Industrials'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Communications Equipment', 'SecurityId': 3445, 'DisplayCode': 'BBRY', 'PricingSymbol': 'BBRY US Equity', 'PriceLast': 6.48, 'SecurityDesc': 'BLACKBERRY LTD', 'Currency': 'USD', 'Country': 'CANADA', 'TradingFactor': 1.0, 'PriceStart': 6.29, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': '', 'SecurityId': 403321, 'DisplayCode': 'ZGSI', 'PricingSymbol': 'ZGSI US Equity', 'PriceLast': 6.5, 'SecurityDesc': 'ZERO GRAVITY SOLUTIONS INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 0.0, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': ''}], 'Holdings': [{'HoldingId': 919049, 'MTDPnLTotal': -36434.0, 'Quantity': -40000.0, 'DTDPnLTotal': -36434.0, 'NetExposure': -3743600.0, 'LastModified': '2016-06-28T12:40:51.527', 'WTDPnLTotal': -36434.0, 'AcuId': 212, 'SecurityId': 2654, 'QuantityTraded': -40000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -36434.0, 'BetaExposure': -4155021.64, 'BookValue': -3743600.0, 'YTDPnLTotal': -36434.0, 'QuantityStart': 0.0, 'ShareEquivalent': -40000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1476408, 'MTDPnLTotal': -40.0, 'Quantity': -1000.0, 'DTDPnLTotal': -40.0, 'NetExposure': -38810.0, 'LastModified': '2016-06-28T12:38:03.72', 'WTDPnLTotal': -40.0, 'AcuId': 212, 'SecurityId': 2660, 'QuantityTraded': -1000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -40.0, 'BetaExposure': -63954.999, 'BookValue': -38810.0, 'YTDPnLTotal': -40.0, 'QuantityStart': 0.0, 'ShareEquivalent': -1000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586376, 'MTDPnLTotal': -30200.0, 'Quantity': 10000.0, 'DTDPnLTotal': -30200.0, 'NetExposure': 64800.0, 'LastModified': '2016-06-28T13:14:42.237', 'WTDPnLTotal': -30200.0, 'AcuId': 212, 'SecurityId': 3445, 'QuantityTraded': 10000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -30200.0, 'BetaExposure': 92178.0, 'BookValue': 64800.0, 'YTDPnLTotal': -30200.0, 'QuantityStart': 0.0, 'ShareEquivalent': 10000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586379, 'MTDPnLTotal': 0.0, 'Quantity': 100.0, 'DTDPnLTotal': 0.0, 'NetExposure': 650.0, 'LastModified': '2016-06-28T14:07:55.58', 'WTDPnLTotal': 0.0, 'AcuId': 212, 'SecurityId': 403321, 'QuantityTraded': 100.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': 0.0, 'BetaExposure': 650.0, 'BookValue': 650.0, 'YTDPnLTotal': 0.0, 'QuantityStart': 0.0, 'ShareEquivalent': 100.0, 'CustodianAccountCode': 'CSPB-739R70'}], 'LiveDate': '2016-06-28T00:00:00', 'RefreshTime': '2016-06-28T21:25:22.8529416Z'}

Which is a dictionary of dictionaries. I want to retrieve "AAPL", which is the value of the key "DisplayCode".

I tried the following but it's not working:

for item in json_data:
    print(item['Holdings']['DisplayCode']) 

Upvotes: 0

Views: 2104

Answers (2)

Mallock
Mallock

Reputation: 545

When I came across this error, my reason was that I was doing something like

    for instrument in self.instruments:
        instrument['bought'] = 0
        instrument['sold'] = 0

Instead, the correct way to achieve this is :

    for instrument_id, instrument in self.instruments.items():
        instrument['bought'] = 0
        instrument['sold'] = 0

Using the items() method from dicts resolved my issue.

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

You need to index the list returned from json_data["Securities"], the dict with the key you want is at index 0:

js = {'Securities': [{'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Computers & Peripherals', 'SecurityId': 2654, 'DisplayCode': 'AAPL', 'PricingSymbol': 'AAPL US Equity', 'PriceLast': 93.59, 'SecurityDesc': 'APPLE INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 92.04, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Air Freight & Logistics', 'SecurityId': 2660, 'DisplayCode': 'AAWW', 'PricingSymbol': 'AAWW US Equity', 'PriceLast': 38.81, 'SecurityDesc': 'ATLAS AIR WORLDWIDE HOLDINGS', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 38.45, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Industrials'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Communications Equipment', 'SecurityId': 3445, 'DisplayCode': 'BBRY', 'PricingSymbol': 'BBRY US Equity', 'PriceLast': 6.48, 'SecurityDesc': 'BLACKBERRY LTD', 'Currency': 'USD', 'Country': 'CANADA', 'TradingFactor': 1.0, 'PriceStart': 6.29, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': '', 'SecurityId': 403321, 'DisplayCode': 'ZGSI', 'PricingSymbol': 'ZGSI US Equity', 'PriceLast': 6.5, 'SecurityDesc': 'ZERO GRAVITY SOLUTIONS INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 0.0, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': ''}], 'Holdings': [{'HoldingId': 919049, 'MTDPnLTotal': -36434.0, 'Quantity': -40000.0, 'DTDPnLTotal': -36434.0, 'NetExposure': -3743600.0, 'LastModified': '2016-06-28T12:40:51.527', 'WTDPnLTotal': -36434.0, 'AcuId': 212, 'SecurityId': 2654, 'QuantityTraded': -40000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -36434.0, 'BetaExposure': -4155021.64, 'BookValue': -3743600.0, 'YTDPnLTotal': -36434.0, 'QuantityStart': 0.0, 'ShareEquivalent': -40000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1476408, 'MTDPnLTotal': -40.0, 'Quantity': -1000.0, 'DTDPnLTotal': -40.0, 'NetExposure': -38810.0, 'LastModified': '2016-06-28T12:38:03.72', 'WTDPnLTotal': -40.0, 'AcuId': 212, 'SecurityId': 2660, 'QuantityTraded': -1000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -40.0, 'BetaExposure': -63954.999, 'BookValue': -38810.0, 'YTDPnLTotal': -40.0, 'QuantityStart': 0.0, 'ShareEquivalent': -1000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586376, 'MTDPnLTotal': -30200.0, 'Quantity': 10000.0, 'DTDPnLTotal': -30200.0, 'NetExposure': 64800.0, 'LastModified': '2016-06-28T13:14:42.237', 'WTDPnLTotal': -30200.0, 'AcuId': 212, 'SecurityId': 3445, 'QuantityTraded': 10000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -30200.0, 'BetaExposure': 92178.0, 'BookValue': 64800.0, 'YTDPnLTotal': -30200.0, 'QuantityStart': 0.0, 'ShareEquivalent': 10000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586379, 'MTDPnLTotal': 0.0, 'Quantity': 100.0, 'DTDPnLTotal': 0.0, 'NetExposure': 650.0, 'LastModified': '2016-06-28T14:07:55.58', 'WTDPnLTotal': 0.0, 'AcuId': 212, 'SecurityId': 403321, 'QuantityTraded': 100.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': 0.0, 'BetaExposure': 650.0, 'BookValue': 650.0, 'YTDPnLTotal': 0.0, 'QuantityStart': 0.0, 'ShareEquivalent': 100.0, 'CustodianAccountCode': 'CSPB-739R70'}], 'LiveDate': '2016-06-28T00:00:00', 'RefreshTime': '2016-06-28T21:25:22.8529416Z'}

print(js["Securities"][0]["DisplayCode"])

That gives you:

AAPL

If it could appear at another index and/or if you want all the DisplayCodes:

for dct in js["Securities"]:
     print(dct["DisplayCode"])

Which would give you:

AAPL
AAWW
BBRY
ZGSI

Upvotes: 4

Related Questions