Eric
Eric

Reputation: 115

How to retrieve these values from a json object in Python?

I'm a beginner Python coder. I've been trying to get the opening stock price for a range of dates using the IEX API.

My json values are showing:

{'AAPL': {'2017-02-09': {'open': 129.1019, 'high': 129.8815, 'low': 128.5821, 'close': 129.8569, 'volume': 28349859.0}, '2017-02-10': {'open': 129.8962, 'high': 130.3669, 'low': 129.4941, 'close': 129.5628, 'volume': 20065458.0}, '2017-02-13': {'open': 130.5042, 'high': 131.2299, 'low': 130.1806, 'close': 130.7101, 'volume': 23035421.0}}}

If I only want to show only the 'open' prices for all the dates, how do I do that?

I've been googling lists and dictionaries but couldn't find anything useful...

from iexfinance import get_historical_data
from datetime import datetime
import json

start = datetime(2017, 2, 9)

end = datetime(2017, 2, 13)

f = get_historical_data("AAPL", start, end, output_format='json')

json_string = json.dumps(f)
json_read = json.loads(json_string)

print(json_read)

Upvotes: 0

Views: 647

Answers (3)

kanishk
kanishk

Reputation: 91

Please try this:

def find_open(stock):

for k,v in stock.items():
    if k == 'open':
        print(stock[k])
    elif isinstance(v, dict):
        find_open(v)

Calling function

find_open({'AAPL': {'2017-02-09': {'open': 129.1019, 'high': 129.8815, 'low': 128.5821, 'close': 129.8569, 'volume': 28349859.0}, '2017-02-10': {'open': 129.8962, 'high': 130.3669, 'low': 129.4941, 'close': 129.5628, 'volume': 20065458.0}, '2017-02-13': {'open': 130.5042, 'high': 131.2299, 'low': 130.1806, 'close': 130.7101, 'volume': 23035421.0}}})

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121476

The iexfinance module you are using can give you a far more convenient format: a Pandas data frame:

df = get_historical_data("AAPL", start, end, output_format='pandas')
print(df.open)

The data is indexed by date, so the df.open column is a pandas Series of opening values by date:

>>> from iexfinance import get_historical_data
>>> from datetime import datetime
>>> start = datetime(2017, 2, 9)
>>> end = datetime(2017, 2, 13)
>>> df = get_historical_data("AAPL", start, end, output_format='pandas')
>>> print(df.open)
date
2017-02-09    129.1019
2017-02-10    129.8962
2017-02-13    130.5042
Name: open, dtype: float64
>>> for open in df.open:
...     print(open)
...
129.1019
129.8962
130.5042

When you use the json format, the module produces a Python dictionary, there is no need to convert to JSON and back again. The format is suitable for JSON serialisation, but you don't need to jump through those hoops. Granted, the developer's choice of format name is confusing there.

To do the same with a dictionary, just loop over all the items for the dictionary referenced by 'AAPL'; keys are dates, and the values are more dictionaries with a key for each column:

f = get_historical_data("AAPL", start, end)
for date, date_entry in f['AAPL'].items():
    print(date, date_entry['open'])

This would give you entries in an dictionary-defined order; you may want to sort by key first:

for date, date_entry in sorted(f['AAPL'].items(), key=lambda kv: kv[0]):
    print(date, date_entry['open'])

Upvotes: 1

Sam Gomena
Sam Gomena

Reputation: 1479

Are you looking for something like this:

for day in json_reads['AAPL']:
    print("{0}: {1}".format(day, json_reads['AAPL'][day]['open']))

Albeit excessively hardcoded, you can probably see the general dictionary access idea.

Upvotes: 0

Related Questions