Hi i’m not an expert and this problem kept me stuck for such a long time I hope that someone here can help me i would like to exctract the value “interestExpense” from the following json file:
JavaScript
x
17
17
1
{'incomeBeforeTax': 17780000000,
2
'minorityInterest': 103000000,
3
'netIncome': 17937000000,
4
'sellingGeneralAdministrative': 5918000000,
5
'grossProfit': 16507000000,
6
'ebit': 10589000000,
7
'endDate': 1640908800,
8
'operatingIncome': 10589000000,
9
'interestExpense': -1803000000,
10
'incomeTaxExpense': -130000000,
11
'totalRevenue': 136341000000,
12
'totalOperatingExpenses': 125752000000,
13
'costOfRevenue': 119834000000,
14
'totalOtherIncomeExpenseNet': 7191000000,
15
'netIncomeFromContinuingOps': 17910000000,
16
'netIncomeApplicableToCommonShares': 17937000000}
17
In this case the result should be -130000000 as a string but i m trying to find a way to create an list(or an array) with all those floats so that i can decide which one to pick, i have no idea how to manipulate this kind of data(json) For example
JavaScript
1
2
1
print(list[0])
2
should return 17780000000(the value associated with incomeBeforeTax) is this actually possible? The output is generated from this code:
JavaScript
1
32
32
1
annual_is_stms=[]
2
url_financials ='https://finance.yahoo.com/quote/{}/financials?p{}'
3
stock= 'F'
4
5
response = requests.get(url_financials.format(stock,stock),headers=headers)
6
soup = BeautifulSoup(response.text,'html.parser')
7
pattern = re.compile(r's--sDatas--s')
8
script_data = soup.find('script',text=pattern).contents[0]
9
10
script_data[:500]
11
12
script_data[-500:]
13
14
start = script_data.find("context")-2
15
json_data =json.loads(script_data[start:-12])
16
json_data['context']['dispatcher']['stores']['QuoteSummaryStore'].keys()
17
18
#all data relative financials
19
20
annual_is=json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['incomeStatementHistory']['incomeStatementHistory']
21
for s in annual_is:
22
statement = {}
23
for key, val in s.items():
24
try:
25
statement[key] = val['raw']
26
except TypeError:
27
continue
28
except KeyError:
29
continue
30
annual_is_stms.append(statement)
31
print(annual_is_stms[0])
32
Advertisement
Answer
If you are using python, you need to include the json module and parse it as an object:
JavaScript
1
11
11
1
import json
2
3
# some JSON:
4
x = '{ "name":"John", "age":30, "city":"New York"}'
5
6
# parse x:
7
y = json.loads(x)
8
9
# the result is a Python dictionary:
10
print(y["age"])
11
Regards
L.