0

I am getting error Expecting value: line 1 column 1 (char 0) when trying to decode JSON.

I'm making an API call through the next code:

import requests
import json

base_url = "https://www.tropicos.org/Name/Search?name={name}&type={data_type}&apikey={api_key}&format={format_file}" #URL from Tropicos page

specie_name = "Quercus alba"
api_key = '###########'
data_type = "exact"
format_file = "json" #I also tried with the "xml" format
nameid_dict = {}

url = base_url
response = requests.get(url.format(name=specie_name, data_type=data_type, api_key=api_key, format_file=format_file))

Until this point, the code works fine. The response status is 200 when I print it, but the error occur in the next code:

if response.status_code == 200:
    data = json.loads(response.content) #I also tried with the next code: data = response.json()
    nameid = data["Result"][0]["NameId"]

    # Store the NameID in the Dictionary
    nameid_dict[name] = nameid
else:
    print(f"Error al realizar la solicitud para {name}")

Traceback:

JSONDecodeError                           Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13580/1752249903.py in <module>
----> 1 data = json.loads(response.content)

C:\ProgramData\Anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    344             parse_int is None and parse_float is None and
    345             parse_constant is None and object_pairs_hook is None and not kw):
--> 346         return _default_decoder.decode(s)
    347     if cls is None:
    348         cls = JSONDecoder

C:\ProgramData\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

C:\ProgramData\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Help, please

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Cope98
  • 1
  • 1
  • 2
  • Possible duplicate? [JSONDecodeError: Expecting value: line 1 column 1 (char 0)](https://stackoverflow.com/q/16573332/4518341) (I'm a bit out of my depth here.) – wjandrea Feb 20 '23 at 23:41
  • 2
    Try printing `response.content`, is it what you expect? Does it look like json? – Jortega Feb 20 '23 at 23:41
  • Welcome to Stack Overflow. "Until this point, the code works fine." Okay, so you were able to get a valid `response`. "but the error occur in the next code:" Okay, so an error is reported almost right away, in `data = json.loads(response.content)`. That does two things, right? It tries to find `response.content`, and then tries to use `json.loads` with that result, right? So - **did you try to check** what `response.content` looks like? Do you know what it **should** look like? **Does it look right**? Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – Karl Knechtel Feb 21 '23 at 00:30

1 Answers1

0

Response from your api not in json format, so json module can't parse it.

To understand what happening, replace line:

data = json.loads(response.content)

to:

print(response.text)
data = json.loads(response.content)

And you will be able to see what's happening. Also as it's simple GET request, try opening same url in browser, it might help you debugging

It might be rate limit or access rights error, probably it will be better to add try/except to avoit same errors later

Alex Kosh
  • 2,206
  • 2
  • 19
  • 18
  • Why do you say it might be an error? OP says they're getting a 200. IIRC, rate limit would return a 429 and no access would be 403. – wjandrea Feb 20 '23 at 23:38
  • 2
    I think this is an error because OP expects valid json, and response is invalid json. Also it's very common for modern APIs to return 200 code with something like "error" – Alex Kosh Feb 20 '23 at 23:39
  • Thanks! You're right about the output format. – Cope98 Feb 24 '23 at 01:37