tgherzog / wbgapi

Python module that makes using the World Bank's API a lot easier and more intuitive.
MIT License
140 stars 31 forks source link

JSON Decode Error/API Response Error #17

Closed julianne-bulatao closed 3 years ago

julianne-bulatao commented 3 years ago

I'm trying to fetch data in a series for a specific country. The series is under the 'Doing Business' database and when I run the code to fetch the data, I receive a JSON Decode Error/API Response Error.

From what I read, it is due to an XML file being returned even though JSON is requested.

What can I do so that I can access the data within the series under 'Doing Business' and any other database series that will give me this error?

Sample Code:

for row in wb.data.fetch('ENF.CONT.COEN.COST.ZS', 'USA'):
    if row['value'] == None:
        print("No data found for: ", row['time'])
    else:
        print(row)

The only database so far that didn't give me this error is the 'Jobs' database. I have also tested all series that are under wb.series.info() and they work as well.

tgherzog commented 3 years ago

You're getting this error because the indicator you want is not in the default database (WDI is the default). You need to specify the database in the request, or set the db global. So either:

for row in wb.data.fetch('ENF.CONT.COEN.COST.ZS', 'USA', db=1):
    ....

or:

wb.db = 1
for row in wb.data.fetch('ENF.CONT.COEN.COST.ZS', 'USA'):
    ...

All relevant API functions include an optional db parameter that defaults to 2 unless you modify the global default.

You can fetch the database list like this:

>>> wb.source.info()
id    name                                                                  code      concepts  lastupdated
----  --------------------------------------------------------------------  ------  ----------  -------------
1     Doing Business                                                        DBS              3  2019-10-23
2     World Development Indicators                                          WDI              3  2021-06-30
3     Worldwide Governance Indicators                                       WGI              3  2020-09-28
5     Subnational Malnutrition Database                                     SNM              3  2016-03-21
6     International Debt Statistics                                         IDS              4  2021-01-21
...