Issue
I'm trying to parse some JSON to get some values, for example the value of 'Count'. I can successfully retrieve the JSON and convert it to text but .get() returns 'NONE' for any values I try to get. The code I am using is:
from urllib import request
from bs4 import BeautifulSoup
import json
url = 'https://irus.jisc.ac.uk/api/sushilite/v1_7/GetReport/?Report=IR1&Release=4&RequestorID=Cambridge&BeginDate=2020-01&EndDate=2020-05&ItemIdentifier=irusuk%3A1861749&Granularity=Monthly&Pretty=Pretty'
html = request.urlopen(url).read()
soup = BeautifulSoup(html,'html.parser')
site_json=json.loads(soup.text)
x = site_json.get('Count')
print(x)
Solution
You should parse the JSON
try:
for values in site_json["ReportResponse"]["Report"]["Report"]["Customer"]["ReportItems"][0]["ItemPerformance"]:
print(values["Instance"]["Count"])
except KeyError as e:
print("Key not found" , e)
Another approach:
if ReportItems
key having multiple values in the list. You can simply iterate the value from ReportItems
key.
try:
for values in site_json["ReportResponse"]["Report"]["Report"]["Customer"]["ReportItems"]:
for performance in values["ItemPerformance"]:
print(performance["Instance"]["Count"])
except KeyError as e:
print("Key not found" , e)
Output:
1001
6273
2128
993
1365
Answered By - Narendra Prasath
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.