Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get specific values out of dictionary with multiple keys in Python

I want to extract multiple ISINs out of a output.json file in python.

The output.json file looks like the following:

{‘A1J780’: {‘ter’: ‘0.20%’, ‘wkn’: ‘A1J780’, ‘isin’: ‘IE00B88DZ566’}, ‘A1J7W9’: {‘ ‘ter’: ‘0.20%’, ‘isin’: ‘IE00B8KMSQ34’}, ‘LYX0VQ’: {‘isin’: ‘LU1302703878’}, ‘A2AMYP’: {‘ter’: ‘0.22%’, ‘savingsPlan’: None, ‘inceptionDate’: ‘02.11.16’, ‘fundSize’: ’48’, ‘isin’: ‘IE00BD34DB16’}}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My current approach is the following:

 with open('output.json') as f:
        data = json.load(f)

    value_list = list()
    for i in data:
         value_list.append(i['isin'])
    print(value_list)

However, I receive the error message:

Traceback (most recent call last):
  File "/Users/placeholder.py", line 73, in <module>
    value_list.append(i['isin'])
                      ~^^^^^^^^
TypeError: string indices must be integers, not 'str'

I would highly appreciate your input!

Thank you in advance!

>Solution :

Use data.values() as target in for loop to iterate over the JSON objects.

data = {
    'A1J780': {'ter': '0.20%', 'wkn': 'A1J780', 'isin': 'IE00B88DZ566'},
    'A1J7W9': {'ter': '0.20%', 'isin': 'IE00B8KMSQ34'}
}
value_list = list()
for i in data.values():
    value_list.append(i['isin'])
print(value_list)

Output:

['IE00B88DZ566', 'IE00B8KMSQ34']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading