-1
URL = "API URL"
response = urllib.request.urlopen(URL)
standards = response.read() #returning type <bytes>
standards = standards.decode('utf-8') #converting type <str>

I actually want to read through the data and extract the values of "referenceNumber" & "title" from the data given below, I have 755 records of same structure. I want to extract the above given fields for each record.

[{"larsCode":0,"referenceNumber":"ST0870","title":"Business support assistant","status":"Withdrawn","url":"https://www.instituteforapprenticeships.org/api/apprenticeshipstandards/0","versionNumber":"0.0","change":"Withdrawn","changedDate":"2019-07-31T00:00:00","earliestStartDate":null,"latestStartDate":null,"latestEndDate":null,"overviewOfRole":"","level":2,"typicalDuration":0,"maxFunding":0,"route":"Business and administration","keywords":["Business","support","assistant","admin","office","office administration"],"jobRoles":[],"entryRequirements":"","assessmentPlanUrl":"","ssa1":"","ssa2":"","version":"0.0","standardInformation":"","occupationalSummary":"","knowledges":[],"behaviours":[],"skills":[],"options":[],"optionsUnstructuredTemplate":[]]

davidism
  • 121,510
  • 29
  • 395
  • 339
Sharjeel shahid
  • 202
  • 1
  • 2
  • 11

1 Answers1

0

The server returns json encoded bytes. Python has a module for json.

You dont need to convert it to str in order to decode the json bytes.

Look at the python documentation for handling json objects.

Specifically json.loads for handling str or json.load for handling file like objects. https://docs.python.org/3/library/json.html#json.loads

Just an fyi, your response is a list of objects so json.loads will return a list of dict.

TeddyBearSuicide
  • 1,377
  • 1
  • 6
  • 11
  • ` URL = "https://www.instituteforapprenticeships.org/api/apprenticeshipstandards" response = urllib.request.urlopen(URL) standards = response.read() standards = json.loads(standards)` – Sharjeel shahid Apr 07 '22 at 23:28
  • The code above shows the following error: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list. – Sharjeel shahid Apr 07 '22 at 23:29
  • That is a server side error. Nothing to do with the code you have posted. Also I got ssl certificate expired errors when connecting to that server. I created a new ssl context that ignores certificates and ran the code and it works fine. – TeddyBearSuicide Apr 08 '22 at 00:08
  • [ { "first_name": "Katie", "last_name": "Rodgers" }, { "first_name": "Naomi", "last_name": "Green" }, ] I'm getting the record in this format, How can I read each record from the list? – Sharjeel shahid Apr 08 '22 at 00:13
  • Its a list, you need to iterate over it. Look into `for loops` in python – TeddyBearSuicide Apr 08 '22 at 00:17