World Bank Data API in Mathematica#

by Vishank Patel

See the World Bank API documentation

These recipe examples were tested on February 8, 2022

1. Get list of country iso2Codes and names#

Define root WorldBank API

urlRoot = "https://api.worldbank.org/v2/";

For obtaining data from the World Bank API, it is helpful to first obtain a list of country codes and names. We will build the API URL using the URLBuild command:

countryURL = URLBuild[{urlRoot, "country"}, {"format" -> "json", "per_page" -> 500}]
https://api.worldbank.org/v2/country?format=json&per_page=500

And then URLExecute to execute the request:

rawCountryData = URLExecute[countryURL] // Short
Output
countryData = rawCountryData[[1, 2]];
countryData // Dataset
Output
countryIso2Code = "iso2Code" /. countryData;
countryIso2Code // Shallow
(* 
"/." is the shorthand notation for "ReplaceAll", which applies rules to each subpart of a given list (countryData in our case). 
Hovering over the /.symbols in Mathematica should display a popup for more information.
*)
Output

Extract country names:

countryName = "name" /. countryData;
countryName // Shallow
Output
countryIso2CodeName = Transpose[{countryIso2Code, countryName}];
countryIso2CodeName // Shallow
Output

So now we know the country iso2codes, which we can use to pull specific indicator data for countries.

2. Compile a Custom Indicator Dataset#

There are many availabe indicators: https://data.worldbank.org/indicator

We wll select three indicators for this example:

  1. Scientific and Technical Journal Article Data = IP.JRN.ARTC.SC

  2. Patent Applications, residents = IP.PAT.RESD

  3. GDP per capita (current US$) Code = NY.GDP.PCAP.CD

Note that these three selected indictaors have a CC-BY 4.0 license. We will compile this indicator data for the United States (US) and United Kingdom (GB)

indicators = {"IP.JRN.ARTC.SC", "IP.PAT.RESD", "NY.GDP.PCAP.CD"}
{IP.JRN.ARTC.SC, IP.PAT.RESD, NY.GDP.PCAP.CD}

Generate the web API URLs we need for U.S.:

USApiURL = {};
For[
 i = 1, i <= Length[indicators], i++,
 AppendTo[USApiURL, 
  URLBuild[{urlRoot, "country", "US", "indicator", 
    indicators[[i]]}, {"format" -> "json", "per_page" -> 500}]]
 ]
USApiURL
{https://api.worldbank.org/v2/country/US/indicator/IP.JRN.ARTC.SC?format=json&per_page=5\
 
>    00, https://api.worldbank.org/v2/country/US/indicator/IP.PAT.RESD?format=json&per_p\
 
>    age=500, https://api.worldbank.org/v2/country/US/indicator/NY.GDP.PCAP.CD?format=js\
 
>    on&per_page=500}

Retrieving Data

USIndicatorData = {};
For[
 j = 1, j <= Length[USApiURL], j++,
 AppendTo[USIndicatorData, URLExecute[USApiURL[[j]]]];
 Pause[1]
 ]
USJournalData = {"date", "value"} /. USIndicatorData[[1, 2]]
Output
USPatentData = {"date", "value"} /. USIndicatorData[[2, 2]]
Output
USGdpData = {"date", "value"} /. USIndicatorData[[3, 2]]
{{2020, 63413.5}, {2019, 65279.5}, {2018, 63064.4}, {2017, 60109.7}, {2016, 58021.4}, 
 
>   {2015, 56863.4}, {2014, 55050.}, {2013, 53106.5}, {2012, 51602.9}, {2011, 49882.6}, 
 
>   {2010, 48466.7}, {2009, 47100.}, {2008, 48382.6}, {2007, 47976.}, {2006, 46298.7}, 
 
>   {2005, 44114.7}, {2004, 41712.8}, {2003, 39496.5}, {2002, 38023.2}, {2001, 37133.2}, 
 
>   {2000, 36334.9}, {1999, 34513.6}, {1998, 32853.7}, {1997, 31459.1}, {1996, 29967.7}, 
 
>   {1995, 28690.9}, {1994, 27694.9}, {1993, 26387.3}, {1992, 25419.}, {1991, 24342.3}, 
 
>   {1990, 23888.6}, {1989, 22857.2}, {1988, 21417.}, {1987, 20038.9}, {1986, 19071.2}, 
 
>   {1985, 18236.8}, {1984, 17121.2}, {1983, 15543.9}, {1982, 14433.8}, {1981, 13976.1}, 
 
>   {1980, 12574.8}, {1979, 11674.2}, {1978, 10564.9}, {1977, 9452.58}, {1976, 8592.25}, 
 
>   {1975, 7801.46}, {1974, 7225.69}, {1973, 6726.36}, {1972, 6094.02}, {1971, 5609.38}, 
 
>   {1970, 5234.3}, {1969, 5032.14}, {1968, 4695.92}, {1967, 4336.43}, {1966, 4146.32}, 
 
>   {1965, 3827.53}, {1964, 3573.94}, {1963, 3374.52}, {1962, 3243.84}, {1961, 3066.56}, 
 
>   {1960, 3007.12}}
USData = Transpose[{USJournalData, USPatentData, USGdpData}];
USData // Dataset
Output

For the United Kingdom:

UKApiURL = {};
For[
 i = 1, i <= Length[indicators], i++,
 AppendTo[UKApiURL, 
  URLBuild[{urlRoot, "country", "GB", "indicator", 
    indicators[[i]]}, {"format" -> "json", "per_page" -> 500}]]
 ]
UKApiURL
{https://api.worldbank.org/v2/country/GB/indicator/IP.JRN.ARTC.SC?format=json&per_page=5\
 
>    00, https://api.worldbank.org/v2/country/GB/indicator/IP.PAT.RESD?format=json&per_p\
 
>    age=500, https://api.worldbank.org/v2/country/GB/indicator/NY.GDP.PCAP.CD?format=js\
 
>    on&per_page=500}
UKIndicatorData = {};
For[
 j = 1, j <= Length[UKApiURL], j++,
 AppendTo[UKIndicatorData, URLExecute[UKApiURL[[j]]]];
 Pause[1]
 ]
UKIndicatorData // Dataset
Output
UKJournalData = {"date", "value"} /. UKIndicatorData[[1, 2]];
UKPatentData = {"date", "value"} /. UKIndicatorData[[2, 2]];
UKGdpData = {"date", "value"} /. UKIndicatorData[[3, 2]];
UKData = Transpose[{UKJournalData, UKPatentData, UKGdpData}];
UKData // Dataset
Output
USJournalData
Output

3. Plot Indicator data#

Create line plots of US/UK Number of Scientific and Technical Journal Articles and Patents by year. As there are no values before the year 2000, we will slice our data for visualizations accordingly.

DateListPlot[{USJournalData[[;; 21]], UKJournalData[[;; 21]]}, 
 PlotMarkers -> Automatic, PlotLegends -> {"US IP", "UK IP"}, PlotTheme -> "Detailed"]
Output
DateListPlot[{USGdpData[[;; 21]], UKGdpData[[;; 21]]}, 
 PlotMarkers -> Automatic, PlotLegends -> {"US GDP", "UK GDP"}, PlotTheme -> "Detailed"]
Output
DateListPlot[{USPatentData[[;; 41]], UKPatentData[[;; 41]]}, 
 PlotMarkers -> Automatic, PlotLegends -> {"US Patents", "UK Patents"}, PlotTheme -> "Detailed"]
Output