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

Time:01-28

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,BriefTitle,Condition&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,BriefTitle,Condition&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()

CodePudding user response:

You can use following code snippet:

import requests, json
response = requests.get('https://clinicaltrials.gov/api/query/study_fields?expr=heart attack&fields=NCTId,BriefTitle,Condition&min_rnk=1&max_rnk=&fmt=json')
jsonResponse = json.loads(response.content)

CodePudding user response:

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.

  • Related