I'm connecting to and extracting a JSON via API, but getting stuck parsing beyond a certain point.
I've been able to parse to the sample level included below. I'd like to extract a list of 'symbols' from the resulting JSON list, but only appear able to do this for individual entities. I'd like to do it directly if at all possible (ie, avoid just using a loop).
Namely, this works:
data[0]['acct']['positions'][1]['i']['symbol']
Returning this:
Out[100]: 'JNC'
But this and this don't:
data[0]['acct']['positions'][:]['i']['symbol']
data[0]['acct']['positions'][0:3]['i']['symbol']
Both returning the following error:
Traceback (most recent call last):
File "<ipython-input-105-72bbee303a08>", line 1, in <module>
data[0]['securitiesAccount']['positions'][:]['instrument']['symbol']
TypeError: list indices must be integers or slices, not str
Sample JSON below:
[{'a': 0.0,
'b': 1.0,
'i': {'desc': 'CASH_EQUIVALENT', 'id': '9ZZZFD104', 'symbol': 'MMDA1'},
'c': 72.64},
{'a': 0.0,
'b': 33.61716,
'i': {'desc': 'EQUITY', 'id': '78464A417', 'symbol': 'JNC'},
'c': 39.59},
{'a': 0.0,
'b': 87.81473,
'i': {'desc': 'EQUITY', 'id': '921937793', 'symbol': 'BVV'},
'c': 19.34}]
[1]you are accessing an element in the list. Where[:]is the entire list itself and[0:3]is a subset of the list. Which need to be accessed with integer indexes.acct,positionsetc. in there. Can you update the example data and example code so that they refer to the same and check whether you still have the issue?