-2

I have this json and I was wondering if someone can help me figure out to just get the '1. open' values for the following json. Using python. Thanks

{
"Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "AAPL",
    "3. Last Refreshed": "2017-12-26",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2017-12-26": {
        "1. open": "170.8000",
        "2. high": "171.4700",
        "3. low": "169.6790",
        "4. close": "170.5700",
        "5. volume": "33106577"
    },
    "2017-12-22": {
        "1. open": "174.6800",
        "2. high": "175.4240",
        "3. low": "174.5000",
        "4. close": "175.0100",
        "5. volume": "16052615"
    },
    "2017-12-21": {
        "1. open": "174.1700",
        "2. high": "176.0200",
        "3. low": "174.1000",
        "4. close": "175.0100",
        "5. volume": "20356826"
    },
Kush Patel
  • 11
  • 1
  • 4
  • 1
    use module `json` to convert it into python's dictionary – furas Dec 27 '17 at 05:27
  • Have you read up on the [`json`](https://docs.python.org/3/library/json.html) module? It's pretty self explanatory. It just converts json text into `list` and `dict` structures and you can retrieve the data as a normal nested dictionary. – SCB Dec 27 '17 at 05:29
  • Possible duplicate of [Parse JSON in Python](https://stackoverflow.com/questions/7771011/parse-json-in-python) – Galen Dec 27 '17 at 05:30

1 Answers1

0

Use json module to convert JSON into python's dictionary. And then it is easy.

data = '''{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "AAPL",
        "3. Last Refreshed": "2017-12-26",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2017-12-26": {
            "1. open": "170.8000",
            "2. high": "171.4700",
            "3. low": "169.6790",
            "4. close": "170.5700",
            "5. volume": "33106577"
        },
        "2017-12-22": {
            "1. open": "174.6800",
            "2. high": "175.4240",
            "3. low": "174.5000",
            "4. close": "175.0100",
            "5. volume": "16052615"
        },
        "2017-12-21": {
            "1. open": "174.1700",
            "2. high": "176.0200",
            "3. low": "174.1000",
            "4. close": "175.0100",
            "5. volume": "20356826"
        }
    }
}'''

import json

data = json.loads(data)

for key, val in data['Time Series (Daily)'].items():
    print(key, val["1. open"])

Result:

2017-12-26 170.8000
2017-12-22 174.6800
2017-12-21 174.1700

If you have problem with structure then you can alwasy use print(item) and print(type(item)) to display item and its type, and print(item.keys()) to see keys if it is dictionary.

print(type(data))
print(data.keys())

# <class 'dict'>
# dict_keys(['Meta Data', 'Time Series (Daily)'])

print(type(data['Time Series (Daily)']))
print(data['Time Series (Daily)'].keys())

# <class 'dict'>
# dict_keys(['2017-12-26', '2017-12-22', '2017-12-21'])
furas
  • 134,197
  • 12
  • 106
  • 148