JSONDecodeError: Expecting value: line 1 column 1 (char 0) / While json parameter include

Question:

I’m trying to retrieve data from https://clinicaltrials.gov/ and althought I’ve specified the format as Json in the request parameter:

fmt=json

the returned value is txt by default.

As a consequence i’m not able to retrieve the response in json()

Good:

import requests
response = requests.get('https://clinicaltrials.gov/api/query/study_fields?expr=heart+attack&fields=NCTId%2CBriefTitle%2CCondition&min_rnk=1&max_rnk=&fmt=json')
response.text

Not Good:

import requests
response = requests.get('https://clinicaltrials.gov/api/query/study_fields?expr=heart+attack&fields=NCTId%2CBriefTitle%2CCondition&min_rnk=1&max_rnk=&fmt=json')
response.json()

Any idea how to turn this txt to json ?

I’ve tried with response.text which is working but I want to retrieve data in Json()

Asked By: Arnaud Fischer

||

Answers:

You should use the JSON package (that is built-in python, so you don’t need to install anything), that will convert the text into a python object (dictionary) using the json.loads() function. Here you can find some examples.

Answered By: Lorenzo Andreasi

You can use following code snippet:

import requests, json
response = requests.get('https://clinicaltrials.gov/api/query/study_fields?expr=heart+attack&fields=NCTId%2CBriefTitle%2CCondition&min_rnk=1&max_rnk=&fmt=json')
jsonResponse = json.loads(response.content)
Answered By: Refet
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.