wbstats icon indicating copy to clipboard operation
wbstats copied to clipboard

"Error: API call executed successfully, but did not return expected json format" when using wb_stat to download data

Open JacopoDior opened this issue 4 months ago • 2 comments

Hello,

when trying to download indicators using the command wb_data I receive the following error: Error: API call executed successfully, but did not return expected json format

For instance when I try to reproduce the line of code available in the help: wb_data("NY.GDP.MKTP.CD")

JacopoDior avatar Feb 18 '24 16:02 JacopoDior

I am having the same problem when using the package. However I try with the package WDI and a similar problem happens:

With wbstats:

library(wbstats)
wb_data(indicator = "NY.GDP.MKTP.CD", 
        country = "BR", 
        start_date = 2021, 
        end_date = 2021)
#> Error: API call executed successfully, but did not return expected json format

Created on 2024-02-18 with reprex v2.1.0

With WDI

library(WDI)
WDI(indicator = "NY.GDP.MKTP.CD", 
    country = "BR", 
    start = 2021, 
    end = 2021)
#> Error in WDI(indicator = "NY.GDP.MKTP.CD", country = "BR", start = 2021, : The following indicators could not be downloaded: NY.GDP.MKTP.CD.
#> 
#> Please make sure that you are running the latest version of the `WDI` package, and that the arguments you are using in the `WDI()` function are valid.
#> 
#> Sometimes, downloads will suddenly stop working, even if nothing has changed in the R code of the WDI package. ("The same WDI package version worked yesterday!") In those cases, the problem is almost certainly related to the World Bank servers or to your internet connection.
#> 
#> You can check if the World Bank web API is currently serving the indicator(s) of interest by typing a URL of this form in your web browser:
#> 
#> https://api.worldbank.org/v2/en/country/all/indicator/NY.GDP.MKTP.CD?format=json&date=:&per_page=32500&page=1

Created on 2024-02-18 with reprex v2.1.0

I think the problem is in te API taking into account that a similar problem happens with the package wbgapi from Python:

import wbgapi as wb

wb.data.DataFrame(series = "NY.GDP.MKTP.CD",
                  economy = "BRA",
                  time = range(2021, 2021))
Traceback (most recent call last): File "C:\Users\Usuario\ANACON~1\Lib\site-packages\requests\models.py", line 971, in json return complexjson.loads(self.text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\ANACON~1\Lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\ANACON~1\Lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\ANACON~1\Lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 9)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\Usuario\ANACON~1\Lib\site-packages\wbgapi_init_.py", line 533, in _queryAPI result = response.json() ^^^^^^^^^^^^^^^ File "C:\Users\Usuario\ANACON~1\Lib\site-packages\requests\models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 8 column 1 (char 9)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "C:\Users\Usuario\ANACON~1\Lib\site-packages\wbgapi\data.py", line 334, in DataFrame for row in fetch(series, economy, time, mrv=mrv, mrnev=mrnev, skipBlanks=skipBlanks, labels=True, skipAggs=skipAggs, numericTimeKeys=numericTimeKeys, params=params, db=db, **dimensions): File "C:\Users\Usuario\ANACON~1\Lib\site-packages\wbgapi\data.py", line 92, in fetch for row in w.refetch(url, keys, params=params_, **values): File "C:\Users\Usuario\ANACON~1\Lib\site-packages\wbgapi_init_.py", line 322, in refetch for row in fetch(url2, params, concepts, lang): File "C:\Users\Usuario\ANACON~1\Lib\site-packages\wbgapi_init_.py", line 281, in fetch (hdr,result) = queryAPI(url) ^^^^^^^^^^^^^^^ File "C:\Users\Usuario\ANACON~1\Lib\site-packages\wbgapi_init_.py", line 535, in _queryAPI raise APIResponseError(url, 'JSON decoding error') wbgapi.APIResponseError: APIError: JSON decoding error (https://api.worldbank.org/v2/en/sources/2/series/NY.GDP.MKTP.CD/country/BRA/time/?per_page=1000&page=1&format=json)

luifrancgom avatar Feb 19 '24 03:02 luifrancgom

Apparently the problem has been solve. Now the data can be downloaded without problem.

luifrancgom avatar Feb 21 '24 14:02 luifrancgom