Related
Here is a part of my Jason file, and I want to read "information" under "runs" -> "results" -> "properties"
I am trying the following:
with open(inputFile, "r") as readFile:
data = json.load(readFile)
print(type(data))
print("Run data type is: ",type(data['runs']))
#print("properties data type is: ", type(data['runs']['properties']))
# error: print("results data type is: ", type(data['runs']['properties']))TypeError: list indices must be integers or slices, not str
for info in data['runs']:
res = info.get('results',{})
#res = info.get('results', {}).get('properties', None)
#Error: AttributeError: 'list' object has no attribute 'get'
#inf = info.get('properties')
print(res)
All the parts that I have commented is not working. and I added also the error message
how can i read "information" in a loop?
{
"$schema" : "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.4.json",
"version" : "2.1.0",
"runs" : [ {
"tool" : { ...},
"artifacts" : [ ...],
"results" : [ {
"ruleId" : "DECL_MISMATCH",
"ruleIndex" : 0,
"message" : {
"text" : "XXXXX"
},
"level" : "error",
"baselineState" : "unchanged",
"rank" : 100,
"kind" : "fail",
"properties" : {
"tags" : [ "databaseId", "metaFamily", "family", "group", "information", "severity", "status", "comment", "justified", "assignedTo", "ticketKey", "color" ],
"databaseId" : 54496,
"metaFamily" : "Defect",
"family" : "Defect",
"group" : "Programming",
"information" : "Impact: High",
"severity" : "Unset",
"status" : "Unreviewed",
"comment" : "",
"justified" : false,
"color" : "RED"
},
"locations" : [ {
"physicalLocation" : {
"artifactLocation" : {
"index" : 0
}
},
"logicalLocations" : [ {
"fullyQualifiedName" : "File Scope",
"kind" : "function"
} ]
} ]
} ]
} ]
}
While you're trying to access the key properties which is inside a list, you have to set the index number. In this json you've posted the index number can be 0. So the code probably should be like this:
with open(inputFile, "r") as readFile:
data = json.load(readFile)
print(type(data))
print("Run data type is: ",type(data['runs']))
#print("properties data type is: ", type(data['runs']['properties']))
# error: print("results data type is: ", type(data['runs']['properties']))TypeError: list indices must be integers or slices, not str
for info in data['runs']:
# res = info.get('results',{})
res = info.get('results', {})[0].get('properties', None)
#inf = info.get('properties')
print(res)
for run in data['runs']:
for result in run['results']:
properties = result['properties']
print("information = {}".format(properties['information']))
I am trying to add data to a pandas dataframe. I am having isses getting to the sub directories. Ideally i would like all of the ratio names like "priceBookValueRatio", "priceToBookRatio", ...ect in the first column, with the dates on the top row going left to right, and the appropriate values under each date. Can anyone help?
Here is my code
def get_jsonparsed_data(ticker):
url = ("https://financialmodelingprep.com/api/v3/financial-ratios/" + ticker)
response = urlopen(url)
data = response.read().decode("utf-8")
value = json.loads(data)
data = value["ratios"]
df = pd.DataFrame(data)
print df
And here is what my data looks like
{
"symbol" : "AAPL",
"ratios" : [ {
"date" : "2019-09-28",
"investmentValuationRatios" : {
"priceBookValueRatio" : "11.1154",
"priceToBookRatio" : "11.1154",
"priceToSalesRatio" : "3.8903",
"priceEarningsRatio" : "18.7109",
"receivablesTurnover" : "5.489",
"priceToFreeCashFlowsRatio" : "17.5607",
"priceToOperatingCashFlowsRatio" : "14.5863",
"priceCashFlowRatio" : "0",
"priceEarningsToGrowthRatio" : "0",
"priceSalesRatio" : "0",
"dividendYield" : "",
"enterpriseValueMultiple" : "1.7966762045884",
"priceFairValue" : "0"
},
"profitabilityIndicatorRatios" : {
"niperEBT" : "0.84056163195765",
"ebtperEBIT" : "1",
"ebitperRevenue" : "0.25266552384174",
"grossProfitMargin" : "0.37817768109035",
"operatingProfitMargin" : "1",
"pretaxProfitMargin" : "0.24572017188497",
"netProfitMargin" : "0.21238094505984",
"effectiveTaxRate" : "0.15943836804235",
"returnOnAssets" : "0.5848",
"returnOnEquity" : "0.6106",
"returnOnCapitalEmployed" : "0.2691",
"nIperEBT" : "0.84056163195765",
"eBTperEBIT" : "1",
"eBITperRevenue" : "0.25266552384174"
},
"operatingPerformanceRatios" : {
"receivablesTurnover" : "5.489",
"payablesTurnover" : "1.2542",
"inventoryTurnover" : "64.5433",
"fixedAssetTurnover" : "6.9606185456686",
"assetTurnover" : "0.76857223883066"
},
"liquidityMeasurementRatios" : {
"currentRatio" : "1.54",
"quickRatio" : "1.3844473032029",
"cashRatio" : "0.46202160464632",
"daysOfSalesOutstanding" : "-9.2636",
"daysOfInventoryOutstanding" : "64.2588",
"operatingCycle" : "",
"daysOfPayablesOutstanding" : "64.8648",
"cashConversionCycle" : ""
},
"debtRatios" : {
"debtRatio" : "0.3192",
"debtEquityRatio" : "1.194",
"longtermDebtToCapitalization" : "0.50361776241806",
"totalDebtToCapitalization" : "0.54422142191553",
"interestCoverage" : "0.0",
"cashFlowToDebtRatio" : "0.64222977037771",
"companyEquityMultiplier" : "3.7410043320661"
},
"cashFlowIndicatorRatios" : {
"operatingCashFlowPerShare" : "15.0267",
"freeCashFlowPerShare" : "12.94",
"cashPerShare" : "10.5773",
"payoutRatio" : "0.251",
"receivablesTurnover" : "5.489",
"operatingCashFlowSalesRatio" : "0.26670997101939",
"freeCashFlowOperatingCashFlowRatio" : "0.84875560231154",
"cashFlowCoverageRatios" : "0.64222977037771",
"shortTermCoverageRatios" : "4.2728448275862",
"capitalExpenditureCoverageRatios" : "6.6118151500715",
"dividendpaidAndCapexCoverageRatios" : "2.8191679531974",
"dividendPayoutRatio" : "0.25551976255972"
}
},
{
"date" : "2018-09-29",
"investmentValuationRatios" : {
"priceBookValueRatio" : "10.1842",
"priceToBookRatio" : "10.1842",
"priceToSalesRatio" : "4.1328",
"priceEarningsRatio" : "18.9226",
"receivablesTurnover" : "6.2738",
"priceToFreeCashFlowsRatio" : "17.563",
"priceToOperatingCashFlowsRatio" : "14.1753",
"priceCashFlowRatio" : "14.375642493446",
"priceEarningsToGrowthRatio" : "18.698887988401",
"priceSalesRatio" : "4.1912065394209",
"dividendYield" : "0.012318046710734",
"enterpriseValueMultiple" : "14.710301181747",
"priceFairValue" : "10.389124295011"
},
"profitabilityIndicatorRatios" : {
"niperEBT" : "0.81657819294131",
"ebtperEBIT" : "1",
"ebitperRevenue" : "0.27448935409176",
"grossProfitMargin" : "0.38343718820008",
"operatingProfitMargin" : "1",
"pretaxProfitMargin" : "0.26694026619477",
"netProfitMargin" : "0.22414202074587",
"effectiveTaxRate" : "0.18342180705869",
"returnOnAssets" : "1.0497",
"returnOnEquity" : "0.5556",
"returnOnCapitalEmployed" : "0.217",
"nIperEBT" : "0.81657819294131",
"eBTperEBIT" : "1",
"eBITperRevenue" : "0.27448935409176"
},
"operatingPerformanceRatios" : {
"receivablesTurnover" : "6.2738",
"payablesTurnover" : "1.2564",
"inventoryTurnover" : "60.2871",
"fixedAssetTurnover" : "6.4302488863064",
"assetTurnover" : "0.72621505229339"
},
"liquidityMeasurementRatios" : {
"currentRatio" : "1.133",
"quickRatio" : "0.99453976140569",
"cashRatio" : "0.22352474359306",
"daysOfSalesOutstanding" : "-8.8176",
"daysOfInventoryOutstanding" : "67.3325",
"operatingCycle" : "",
"daysOfPayablesOutstanding" : "76.8054",
"cashConversionCycle" : ""
},
"debtRatios" : {
"debtRatio" : "0.313",
"debtEquityRatio" : "1.0685",
"longtermDebtToCapitalization" : "0.46661721806832",
"totalDebtToCapitalization" : "0.51655010603258",
"interestCoverage" : "0.0",
"cashFlowToDebtRatio" : "0.67637989919901",
"companyEquityMultiplier" : "3.4133013523477"
},
"cashFlowIndicatorRatios" : {
"operatingCashFlowPerShare" : "15.6263",
"freeCashFlowPerShare" : "9.924",
"cashPerShare" : "5.2293",
"payoutRatio" : "0.226",
"receivablesTurnover" : "6.2738",
"operatingCashFlowSalesRatio" : "0.29154916319961",
"freeCashFlowOperatingCashFlowRatio" : "0.8280729395356",
"cashFlowCoverageRatios" : "0.67637989919901",
"shortTermCoverageRatios" : "3.7321187584345",
"capitalExpenditureCoverageRatios" : "5.8164200405619",
"dividendpaidAndCapexCoverageRatios" : "2.8652728954672",
"dividendPayoutRatio" : "0.2303337756799"
}
},
{
"date" : "2017-09-30",
"investmentValuationRatios" : {
"priceBookValueRatio" : "5.9086",
"priceToBookRatio" : "5.9086",
"priceToSalesRatio" : "3.4657",
"priceEarningsRatio" : "16.5922",
"receivablesTurnover" : "7.0564",
"priceToFreeCashFlowsRatio" : "15.4994",
"priceToOperatingCashFlowsRatio" : "12.37",
"priceCashFlowRatio" : "12.166429629599",
"priceEarningsToGrowthRatio" : "16.160760748713",
"priceSalesRatio" : "3.4086956688842",
"dividendYield" : "0.016341413728755",
"enterpriseValueMultiple" : "12.106846738693",
"priceFairValue" : "5.8292161925369"
},
"profitabilityIndicatorRatios" : {
"niperEBT" : "0.75443523849647",
"ebtperEBIT" : "1",
"ebitperRevenue" : "0.27957894553164",
"grossProfitMargin" : "0.38469860491899",
"operatingProfitMargin" : "1",
"pretaxProfitMargin" : "0.2676042820873",
"netProfitMargin" : "0.21092420845075",
"effectiveTaxRate" : "0.24556476150353",
"returnOnAssets" : "0.7847",
"returnOnEquity" : "0.3607",
"returnOnCapitalEmployed" : "0.1752",
"nIperEBT" : "0.75443523849647",
"eBTperEBIT" : "1",
"eBITperRevenue" : "0.27957894553164"
},
"operatingPerformanceRatios" : {
"receivablesTurnover" : "7.0564",
"payablesTurnover" : "1.2897",
"inventoryTurnover" : "65.6173",
"fixedAssetTurnover" : "6.7854838232247",
"assetTurnover" : "0.61077110404749"
},
"liquidityMeasurementRatios" : {
"currentRatio" : "1.276",
"quickRatio" : "1.089670085504",
"cashRatio" : "0.20125181026445",
"daysOfSalesOutstanding" : "-12.5636",
"daysOfInventoryOutstanding" : "56.8007",
"operatingCycle" : "",
"daysOfPayablesOutstanding" : "70.4447",
"cashConversionCycle" : ""
},
"debtRatios" : {
"debtRatio" : "0.3082",
"debtEquityRatio" : "0.863",
"longtermDebtToCapitalization" : "0.42034732372197",
"totalDebtToCapitalization" : "0.46322584262014",
"interestCoverage" : "0.0",
"cashFlowToDebtRatio" : "0.55519536652835",
"companyEquityMultiplier" : "2.7999060031183"
},
"cashFlowIndicatorRatios" : {
"operatingCashFlowPerShare" : "12.3101",
"freeCashFlowPerShare" : "9.779",
"cashPerShare" : "3.8888",
"payoutRatio" : "0.259",
"receivablesTurnover" : "7.0564",
"operatingCashFlowSalesRatio" : "0.28017222576058",
"freeCashFlowOperatingCashFlowRatio" : "0.80613468275594",
"cashFlowCoverageRatios" : "0.55519536652835",
"shortTermCoverageRatios" : "3.476695718075",
"capitalExpenditureCoverageRatios" : "5.1582202232752",
"dividendpaidAndCapexCoverageRatios" : "2.5465900079302",
"dividendPayoutRatio" : "0.26408967756613"
}
},
pandas cant construct a DataFrame from an arbitrary nested dict. You need to pass the data through in a standard format it can parse.
One way to do this is to create a list of single level dicts with keys and values and then construct the DataFrame from this
def clean_data(d):
ret = {}
ret['date'] = d['date']
for outer_key, rec in d.items():
if outer_key != 'date':
for k,v in rec.items():
ret[k]= v
return ret
cleaned_data = [clean_data(d) for d in data['ratios']]
df = pd.DataFrame.from_records(cleaned_data, index='date')
df = df.transpose()
will give you
I am upgrading from python 2 to 3. This code works in python 2 but not in 3. When I run in python 3 it seems to not get the data into an actual json format plus add's \n and b'.
I believe my python 3 is writing my json file wrongly.
Code to extract json from web url:
def WebService_As_Source(Source_Id):
dst_path = SOURCECONFIG.GLOBAL_WorkPath
bdate = SOURCECONFIG.GLOBAL_DATE
print ("Extracting from Web Service...\t\t" + str(datetime.datetime.now()))
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
uid = 'stack' #save lan your userid ######################################################## enter UserId
pwd = 'overflow'#save your lan password ######################################################## enter Password
top_level_url = SOURCECONFIG.WebServices_URL(Source_Id)
password_mgr.add_password(None, top_level_url, uid, pwd)
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)
# use the opener to fetch a URL
opener.open(top_level_url)
# Now all calls to urllib2.urlopen use our opener.
urllib.request.install_opener(opener)
req = urllib.request.Request(top_level_url)
response = urllib.request.urlopen(req)
data = response.readlines()
otf = open(os.path.join(dst_path + Source_Id+".json"), "w+") # open text file
rowcount = 0
for line in data:
rowcount = rowcount + 1
otf.write(str(line))
otf.close()
print(Source_Id +" json extracted.\t\t"+ str(datetime.datetime.now()))
Sample of my actual Json file python 3 produces:
b'[ {\n'b' "filterFlag" : "",\n'b' "lookup" : "",\n'b' "rule" : "",\n'b' "prefix" : "",\n'b' "validBDRAppName" : "",\n'b' "vendor" : {\n'b' "bookId" : "40302539",\n'b' "bookName" : "NYC",\n'b' "bookStatus" : "ACTIVE",\n'b' "commProductType" : "",\n'b' "businessDate" : "2019-08-05",\n'b' "endOfDay" : null,\n'b' "excludeFromAggregation" : "FALSE",\n'b' "geoLocation" : "",\n'b' "isHoliday" : "",\n'b' "isOSFIBook" : false,\n'b' "legalEntity" : "",\n'b' "location" : "",\n'b' "logicalDate" : "",\n'b' "regulatoryType" : "Trading",\n'b' "reportingLineBookName" : "NYC",\n'b' "reportingLinePathName" : "super/user",\n'b' "riskFilterType" : "USA",\n'b' "statusId" : "",\n'b' "transit" : "",\n'b' "l8n" : ""\n'b' },\n'b' "bdr" : {\n'b' "bookId" : "7447",\n'b' "bookName" : "NY",\n'b' "bookTransit" : "92218",\n'b' "bookStatus" : "ACTIVE",\n'b' "owner" : "",\n'b' "empId" : "",\n'b' "purpose" : "Trading",\n'b' "appName" : "STRATEGY",\n'b' "appCode" : "STRATEGY",\n'b' "transitDesc" : "TOR",\n'b' "appCategory" : "Front Office",\n'b' "bookAppId" : "49512",\n'b' "bookAppName" : "NY",\n'b' "deskName" : "USA",\n'b' "product" : "",\n'b' "asOfDate" : "2019-08-05",\n'b' "legalEntity" : "CANADA",\n'b' "bookAppSecondaryName" : "NY",\n'b' "strategy" : "NY",\n'b' "lhu" : "FCC3",\n'b' "masterBookName" : "NY"\n'b' }\n'b'}, {\n'b' "filterFlag" : "",\n'b' "lookup" : "",\n'b' "rule" : "",\n'b' "prefix" : "",\n'b' "validBDRAppName" : "",\n'b' "vendor" : {\n'b' "bookId" : "40296540",\n'b' "bookName" : "LDN",\n'b' "bookStatus" : "ACTIVE",\n'b' "commProductType" : "",\n'b' "businessDate" : "2019-08-05",\n'b' "endOfDay" : null,\n'b' "excludeFromAggregation" : "FALSE",\n'b' "geoLocation" : "",\n'b' "isHoliday" : "",\n'b' "isOSFIBook" : false,\n'b' "legalEntity" : "",\n'b' "location" : "",\n'b' "logicalDate" : "",\n'b' "regulatoryType" : "Trading",\n'b' "reportingLineBookName" : "LDN",\n'b' "reportingLinePathName" : "stack/overflow",\n'b' "riskFilterType" : "NONE",\n'b' "statusId" : "",\n'b' "transit" : "",\n'b' "l8n" : ""\n'b' },\n'b'
Sample of my Json file python 2 products:
I ran my code to grab the json from the url in python 2 and it gives me the data in the actual json format and doesnt add the b''s and \n's.
[ {
"filterFlag" : "",
"lookup" : "",
"rule" : "",
"prefix" : "",
"validBDRAppName" : "",
"vendor" : {
"bookId" : "40302539",
"bookName" : "NYC",
"bookStatus" : "ACTIVE",
"commProductType" : "",
"businessDate" : "2019-08-06",
"endOfDay" : null,
"excludeFromAggregation" : "FALSE",
"geoLocation" : "",
"isHoliday" : "",
"isOSFIBook" : false,
"legalEntity" : "",
"location" : "",
"logicalDate" : "",
"regulatoryType" : "Trading",
"reportingLineBookName" : "NYC",
"reportingLinePathName" : "super/user",
"riskFilterType" : "USA",
"statusId" : "",
"transit" : "",
"l8n" : ""
},
"bdr" : {
"bookId" : "7447",
"bookName" : "NY",
"bookTransit" : "92218",
"bookStatus" : "ACTIVE",
"owner" : "",
"empId" : "",
"purpose" : "Trading",
"appName" : "STRATEGY",
"appCode" : "STRATEGY",
"transitDesc" : "TOR",
"appCategory" : "Front Office",
"bookAppId" : "49512",
"bookAppName" : "NY",
"deskName" : "USA",
"product" : "",
"asOfDate" : "2019-08-06",
"legalEntity" : "CANADA",
"bookAppSecondaryName" : "NY",
"strategy" : "NY",
"lhu" : "FCC3",
"masterBookName" : "NY"
}
}, {
"filterFlag" : "",
"lookup" : "",
"rule" : "",
"prefix" : "",
"validBDRAppName" : "",
"vendor" : {
"bookId" : "40296540",
"bookName" : "LDN",
"bookStatus" : "ACTIVE",
"commProductType" : "",
"businessDate" : "2019-08-06",
"endOfDay" : null,
"excludeFromAggregation" : "FALSE",
"geoLocation" : "",
"isHoliday" : "",
"isOSFIBook" : false,
"legalEntity" : "",
"location" : "",
"logicalDate" : "",
"regulatoryType" : "Trading",
"reportingLineBookName" : "LDN",
"reportingLinePathName" : "stack/overflow",
"riskFilterType" : "NONE",
"statusId" : "",
"transit" : "",
"l8n" : ""
can anyone help with this?
The problem is that response.readlines() (where response = urllib.request.urlopen(url)) returns a list of bytes. In python 2 bytes and str are the same thing, but on python 3 this is no longer true. So when you did
otf.write(str(line))
the str() call was a no-op on python 2, but on python 3 you called str on a bytes object. This is never what you want to do:
>>> import urllib
... resp = urllib.request.urlopen('https://stackoverflow.com')
... dat = resp.readlines()
... first_line = dat[0]
... print(type(first_line))
... print(repr(first_line))
... print(repr(str(first_line)))
<class 'bytes'>
b'<!DOCTYPE html>\r\n'
"b'<!DOCTYPE html>\\r\\n'"
As you can see, the first line is a bytes object, and str(first_line) is a string that literally starts with a b and some single quotes.
Instead what you have to do is decode your bytes according to its corresponding encoding. I'm not very familiar with web things so I don't know what the best way is to correctly guess the encoding used by the website you're making requests to, but I do know that the third-party requests library can give you a usually correctly decoded json directly from the response.
If with urllib you have to do the decoding manually you need something like
otf.write(line.decode('utf8'))
Python 2 didn't differentiate between byte strings and unicode strings. Python 3 does, which is what the b'' is denoting.
This line
data = response.readlines()
could be
data = response.read().decode(response.headers.get_content_charset()).split('/n')
which should figure out the proper encoding, as per this answer
Trying to parse a Json structure in python and Adding a new value with key 'cat':
data = []
for x in a:
for y in x['Hp'].values():
for z in y:
for k in z['abc']['xyz']:
for m in data:
det = m['response']
// Some processing with det whose output is stored in s
k['cat'] = s
print x
However when x is print only the last value is being appended onto the whole dictionary, wheras there are different values for s.
Its obvious that the 'cat' key is being overwritten everytime the loop rounds,but can't find a way to make it right
Below is a sample Json structure:
{
"_id" : ObjectId("asdasda156121s"),
"Hp" : {
"bermud" : [
{
"abc" : {
"gfh" : 1,
"fgh" : 0.0,
"xyz" : [
{
"kjl" : "0",
"bnv" : 0,
}
],
"xvc" : "bv",
"hgth" : "INnn",
"sdf" : 0,
}
}
},
{
"abc" : {
"gfh" : 1,
"fgh" : 0.0,
"xyz" : [
{
"kjl" : "0",
"bnv" : 0,
}
],
"xvc" : "bv",
"hgth" : "INnn",
"sdf" : 0,
}
}
},
..
If you want to store all values change
k['cat'] = s
to
if 'cat' in k.keys():
k['cat'] += s
else:
k['cat'] = s
If you want to store only the first one change
k['cat'] = s
to
if 'cat' not in k.keys():
k['cat'] = s
I am wondering what I am doing wrong when trying to print the data of name of the following code in python.
import urllib.request, json
with urllib.request.urlopen("<THIS IS A URL IN THE ORIGINAL SCRIPT>") as url:
data = json.loads(url.read().decode())
print (data['Departure']['Product']['name'])
print (data['Departure']['Stops']['Stop'][0]['depTime'])
And this is the api I am fetching the data from:
{
"Departure" : [ {
"Product" : {
"name" : "Länstrafik - Buss 201",
"num" : "201",
"catCode" : "7",
"catOutS" : "BLT",
"catOutL" : "Länstrafik - Buss",
"operatorCode" : "254",
"operator" : "JLT",
"operatorUrl" : "http://www.jlt.se"
},
"Stops" : {
"Stop" : [ {
"name" : "Gislaved Lundåkerskolan",
"id" : "740040260",
"extId" : "740040260",
"routeIdx" : 12,
"lon" : 13.530096,
"lat" : 57.298178,
"depTime" : "20:55:00",
"depDate" : "2019-03-05"
}
data["Departure"] is a list, and you are indexing into it like it's a dictionary.
You wrote the dictionary sample confusingly. Here's how I think it looks:
d = {
"Departure" : [ {
"Product" : {
"name" : "Länstrafik - Buss 201",
"num" : "201",
"catCode" : "7",
"catOutS" : "BLT",
"catOutL" : "Länstrafik - Buss",
"operatorCode" : "254",
"operator" : "JLT",
"operatorUrl" : "http://www.jlt.se"
},
"Stops" : {
"Stop" : [ {
"name" : "Gislaved Lundåkerskolan",
"id" : "740040260",
"extId" : "740040260",
"routeIdx" : 12,
"lon" : 13.530096,
"lat" : 57.298178,
"depTime" : "20:55:00",
"depDate" : "2019-03-05"
}]}}]}
And here's how you can print depTime
print(d["Departure"][0]["Stops"]["Stop"][0]["depTime"])
The important part you missed is d["Departure"][0] because d["Departure"] is a list.
As Kyle said in the previous answer, data["Departure"] is a list, but you're trying to use it as a dictionary. There are 2 possible solutions.
Change data["Departure"]["Stops"]["Stop"] etc. to data["Departure"][0]["Stops"]["Stop"] etc.
Change the JSON file to make departure into a dictionary, which would allow you to keep your original code. This would make the final JSON snippet look like this:
"Departure" : {
"Product" : {
"name" : "Länstrafik - Buss 201",
"num" : "201",
"catCode" : "7",
"catOutS" : "BLT",
"catOutL" : "Länstrafik - Buss",
"operatorCode" : "254",
"operator" : "JLT",
"operatorUrl" : "http://www.jlt.se"
},
"Stops" : {
"name" : "Gislaved Lundåkerskolan",
"id" : "740040260",
"extId" : "740040260",
"routeIdx" : 12,
"lon" : 13.530096,
"lat" : 57.298178,
"depTime" : "20:55:00",
"depDate" : "2019-03-05"
}
}