title: factbook.json - Turn the World Factbook into Open (Structured) Data
- What's the World Factbook?
- Country Profile Example - Austria (au) :: Europe
- Country Trivia (Taxonomy)
- Countries - Politics Trivia
- Divide n Conquer - Continents n Regions Trivia (Taxonomy)
- Europe Trivia (Taxonomy)
- Facts - Did You Know?
- History - Milestones
- History - Let's welcome factbook.json!
- factbook.json - Example
- Country Profiles - What's Included?
- Inside factbook.json - How it works
- Step 1: Beautiful HTML
- Step 2: Convert HTML to JSON
- factbook.json - Philosophy
- Case Study - How to use?
- World Alamanc
- factbook.sql
The World Factbook [1][2] published by the Central Intelligence Agency (CIA) offers free country profiles in the public domain (that is, no copyright(s), no rights reserved).
Note: All country profiles use the "official" two-letter GEC (formerly FIPS) codes and NOT the ISO codes (you might be used to) e.g.
au
for Austria (and notat
),gm
for Germany (and notde
), and so on.
(Source: www.cia.gov/the-world-factbook/countries/austria)
Q: How many "countries" in the world?
A: 261
- 195 sovereign countries
- 52 territories n dependencies
- 6 miscellaneous
- 2 others
- 5 oceans
- 1 world
Palestine NOT included; includes two entries: Gaza Strip (gz), West Bank (we) - NOT recognized as a sovereign country by the US.
Tawain (tw) categorized in others - US acknowledges Beijing's One-China policy e.g. Tawain is part of China (and NOT a sovereign country).
Burma (bm) not renamed to Myanmar - new name NOT recognized by the US.
European Union (ee) included in others.
And much more.
Q: How many "continents/regions" in the world?
A: 11
- North America (7)
- Central America & Caribbean (33)
- South America (14)
- Europe (55)
- Africa (56)
- Middle East (19)
- South Asia (9)
- Central Asia (6)
- East & Southeast Asia (22)
- Australia/Oceania (30)
- Antartica (4)
Q: How many "countries" in Europe?
A: 55
- Greenland - Part of Europe? (no, in North America)
- Cyprus - Part of Europe? (yes)
- Turkey - Part of Europe? (no, in Middle East)
- Russia - Part of Europe? (no, in Central Asia)
- Uzbekistan - Part of Europe? (no, in Central Asia)
The World Factbook is one of the US Government's most accessed publications.
The World Factbook, produced for US policymakers and coordinated throughout the US Intelligence Community, presents the basic realities about the world in which we live. We share these facts with the people of all nations in the belief that knowledge of the truth underpins the functioning of free societies.
Who uses The World Factbook?
A wide variety of folks including US Government officials, researchers, news organizations, corporations, geographers, teachers, professors, librarians, and students. In short, anyone looking for an expansive body of international data on a recently updated Web site.
The World Factbook is a unique reference in that it is updated continuously - on average, every week.
Information in The Factbook is collected from - and coordinated with - a wide variety of US Government agencies, as well as from hundreds of published sources.
(Source: the-world-factbook/about/did-you-know)
The United States has carried on foreign intelligence activities since the days of George Washington but only since World War II have they been coordinated on a government-wide basis. Three programs have highlighted the development of coordinated basic intelligence since that time: (1) the Joint Army Navy Intelligence Studies (JANIS), (2) the National Intelligence Survey (NIS), and (3) The World Factbook.
During World War II, intelligence consumers realized that the production of basic intelligence by different components of the US Government resulted in a great duplication of effort and conflicting information. The Japanese attack on Pearl Harbor in 1941 brought home to leaders in Congress and the executive branch the need for integrating departmental reports to national policymakers. Detailed and coordinated information was needed not only on such major powers as Germany and Japan, but also on places of little previous interest. In the Pacific Theater, for example, the Navy and Marines had to launch amphibious operations against many islands about which information was unconfirmed or nonexistent. Intelligence authorities resolved that the United States should never again be caught unprepared.
In 1943, Gen. George B. Strong (G-2), Adm. H. C. Train (Office of Naval Intelligence - ONI), and Gen. William J. Donovan (Director of the Office of Strategic Services - OSS) decided that a joint effort should be initiated. A steering committee was appointed on 27 April 1943 that recommended the formation of a Joint Intelligence Study Publishing Board to assemble, edit, coordinate, and publish the Joint Army Navy Intelligence Studies (JANIS). JANIS was the first interdepartmental basic intelligence program to fulfill the needs of the US Government for an authoritative and coordinated appraisal of strategic basic intelligence. Between April 1943 and July 1947, the board published 34 JANIS studies. JANIS performed well in the war effort, and numerous letters of commendation were received, including a statement from Adm. Forrest Sherman, Chief of Staff, Pacific Ocean Areas, which said, "JANIS has become the indispensable reference work for the shore-based planners."
The need for more comprehensive basic intelligence in the postwar world was well expressed in 1946 by George S. Pettee, a noted author on national security. He wrote in The Future of American Secret Intelligence (Infantry Journal Press, 1946, page 46) that world leadership in peace requires even more elaborate intelligence than in war. "The conduct of peace involves all countries, all human activities - not just the enemy and his war production."
(Source: the-world-factbook/about/history)
1962 The first classified Factbook was published in August 1962.
1971 The first unclassified version was published in June 1971.
1981 Publication becomes an annual product and is renamed The World Factbook. A total of 165 nations are covered on 225 pages.
1992 Twenty new successor state entries replace those of the Soviet Union and Yugoslavia. New countries are respectively: Armenia, Azerbaijan, Belarus, Estonia, Georgia, Kazakhstan, Kyrgyzstan, Latvia, Lithuania, Moldova, Russia, Tajikistan, Turkmenistan, Ukraine, Uzbekistan; and Bosnia and Hercegovina, Croatia, Macedonia, Serbia and Montenegro, Slovenia. Number of nations in the Factbook rises to 188.
1994 The World Factbook is first produced on CD-ROM.
1997 The World Factbook introduced onto the Internet. A special printed edition prepared for the CIA's 50th anniversary.
1998 Last year for the production of CD-ROM versions of the Factbook.
2012 Size of the printed Factbook's 50th anniversary edition reaches 847 pages.
(Source: the-world-factbook/about/history)
2015
A nobody in Austria (au) introduces the World Factbook to the free world and open structured data. Let's welcome factbook.json (and the factbook page parser)!
See /factbook/factbook
and /factbook/factbook.json
on GitHub.
Example - Austria - au.json:
{
"Introduction": {
"Background": {
"text": "Once the center of power for the large Austro-Hungarian Empire,
Austria was reduced to a small republic after its defeat in World War..."
}
},
"Geography": {
"Location": {
"text": "Central Europe, north of Italy and Slovenia"
},
"Geographic coordinates": {
"text": "47 20 N, 13 20 E"
},
"Map references": {
"text": "Europe"
},
"Area": {
"total": {
"text": "83,871 sq km"
},
"land": {
"text": "82,445 sq km"
},
"water": {
"text": "1,426 sq km"
}
},
"Area - comparative": {
"text": "about the size of South Carolina...",
},
"Land boundaries": {
"total": {
"text": "2,524 km"
},
"border countries": {
"text": "Czech Republic 402 km, Germany 801 km, Hungary 321 km, Italy 404 km,
Liechtenstein 34 km, Slovakia 105 km, Slovenia 299 km, Switzerland 158 km"
}
},
"Climate": {
"text": "temperate; continental, cloudy; cold winters with frequent rain and
some snow in lowlands and snow in mountains;
moderate summers with occasional showers"
},
"Elevation extremes": {
"lowest point": {
"text": "Neusiedler See 115 m"
},
"highest point": {
"text": "Grossglockner 3,798 m"
}
},
"Natural resources": {
"text": "oil, coal, lignite, timber, iron ore, copper, zinc, antimony,
magnesite, tungsten, graphite, salt, hydropower"
},
...
Example Sections and Subsections for Austria (au):
- Introduction
- Background
- Geography
- Location
- Geographic coordinates
- Map references
- Area
- Area - comparative
- Land boundaries
- Coastline
- Maritime claims
- Climate
- Terrain
- Elevation
- Natural resources
- Land use
- Irrigated land
- Population - distribution
- Natural hazards
- Environment - current issues
- Environment - international agreements
- Geography - note
- People and Society
- Population
- Nationality
- Ethnic groups
- Languages
- Religions
- Age structure
- Dependency ratios
- Median age
- Population growth rate
- Birth rate
- Death rate
- Net migration rate
- Population distribution
- Urbanization
- Major urban areas - population
- Sex ratio
- Mother's mean age at first birth
- Maternal mortality rate
- Infant mortality rate
- Life expectancy at birth
- Total fertility rate
- Contraceptive prevalence rate
- Health expenditures
- Physicians density
- Hospital bed density
- Drinking water source
- Sanitation facility access
- HIV/AIDS - adult prevalence rate
- HIV/AIDS - people living with HIV/AIDS
- HIV/AIDS - deaths
- Obesity - adult prevalence rate
- Education expenditures
- School life expectancy (primary to tertiary education)
- Unemployment, youth ages 15-24
- Government
- Country name
- Government type
- Capital
- Administrative divisions
- Independence
- National holiday
- Constitution
- Legal system
- International law organization participation
- Citizenship
- Suffrage
- Executive branch
- Legislative branch
- Judicial branch
- Political parties and leaders
- Political pressure groups and leaders
- International organization participation
- Diplomatic representation in the US
- Diplomatic representation from the US
- Flag description
- National symbol(s)
- National anthem
- Economy
- Economy - overview
- GDP (purchasing power parity)
- GDP (official exchange rate)
- GDP - real growth rate
- GDP - per capita (PPP)
- Gross national saving
- GDP - composition, by end use
- GDP - composition, by sector of origin
- Agriculture - products
- Industries
- Industrial production growth rate
- Labor force
- Labor force - by occupation
- Unemployment rate
- Population below poverty line
- Household income or consumption by percentage share
- Distribution of family income - Gini index
- Budget
- Taxes and other revenues
- Budget surplus (+) or deficit (-)
- Public debt
- Fiscal year
- Inflation rate (consumer prices)
- Commercial bank prime lending rate
- Stock of narrow money
- Stock of broad money
- Stock of domestic credit
- Market value of publicly traded shares
- Current account balance
- Exports
- Exports - commodities
- Exports - partners
- Imports
- Imports - commodities
- Imports - partners
- Reserves of foreign exchange and gold
- Debt - external
- Stock of direct foreign investment - at home
- Stock of direct foreign investment - abroad
- Exchange rates
- Energy
- Electricity access
- Electricity - production
- Electricity - consumption
- Electricity - exports
- Electricity - imports
- Electricity - installed generating capacity
- Electricity - from fossil fuels
- Electricity - from nuclear fuels
- Electricity - from hydroelectric plants
- Electricity - from other renewable sources
- Crude oil - production
- Crude oil - exports
- Crude oil - imports
- Crude oil - proved reserves
- Refined petroleum products - production
- Refined petroleum products - consumption
- Refined petroleum products - exports
- Refined petroleum products - imports
- Natural gas - production
- Natural gas - consumption
- Natural gas - exports
- Natural gas - imports
- Natural gas - proved reserves
- Carbon dioxide emissions from consumption of energy
- Communications
- Telephones - fixed lines
- Telephones - mobile cellular
- Telephone system
- Broadcast media
- Internet country code
- Internet users
- Transportation
- National air transport system
- Civil aircraft registration country code prefix
- Airports
- Airports - with paved runways
- Airports - with unpaved runways
- Heliports
- Pipelines
- Railways
- Roadways
- Waterways
- Merchant marine
- Ports and terminals
- Military and Security
- Military branches
- Military service age and obligation
- Military expenditures
- Transnational Issues
- Disputes - international
- Refugees and internally displaced persons
- Illicit drugs
Step 1: Fetch Country Profile Page (in HTML)
Step 2: Remove "Chrome" - all headers, footers, scripts, etc. - keep the "raw" profile data (in HTML)
Step 3: Cleanup the profile data (in HTML) e.g.
All (style) classes get stripped except category
, category_data
.
All country comparison links get stripped. All field reference links
and images get stripped. The audio player (for the national anthem) gets stripped.
All gallery photos gets stripped and so on and so forth.
Resulting in:
<h2>Introduction</h2>
<h3>Background:</h3>
<div class=category_data>Once the center of power for the large Austro-Hungarian Empire,
Austria was reduced to a small republic after its defeat ...
A prosperous, democratic country, Austria entered the EU Economic and Monetary Union in 1999.</div>
<h2>Geography</h2>
<h3>Location:</h3>
<div class=category_data>Central Europe, north of Italy and Slovenia</div>
<h3>Geographic coordinates:</h3>
<div class=category_data>47 20 N, 13 20 E</div>
<h3>Map references:</h3>
<div class=category_data>Europe</div>
<h3>Area:</h3>
<div><span class=category>total: </span><span class=category_data>83,871 sq km</span></div>
<div><span class=category>land: </span><span class=category_data>82,445 sq km</span></div>
<div><span class=category>water: </span><span class=category_data>1,426 sq km</span></div>
...
- Heading 2 (
h2
) gets converted to a section. - Heading 3 (
h3
) gets converted to a subsection. - Here be dragons. Now the unknown - everythig is possible part ;-)
- Data entry might be a simple entry or an entry with a note or nested entry or nested entry with a note or a sequence of simple entries and so on.
Don't be "smarter" than the CIA - is it possible? ;-)
- Use the same country code as the CIA.
- Use the same region for countries as the CIA.
- Use the same section names as the CIA.
- Use the same subsection names as the CIA (including "inconsistencies").
- Use the same data as the CIA (including "known" typos /errors).
Build yourself your very own World Alamanc in 20 Lines of Script
require 'factbook'
TEMPLATE = <<EOS
### <%= names %>
<%= page.name_long=='none' ? '\-' : page.name_long %> › <%= page.map %> -- <%= page.location %> <br>
<%= page.capital %> • <%= page.area %> • pop. <%= page.population %>
**Languages:** <%= page.languages %>
**Major cities:** <%= page.major_cities %>
**Ethnic groups:** <%= page.ethnic_groups %>
**Religions:** <%= page.religions %>
**Independence:** <%= page.independence %>
**Internet:** `<%= page.internet %>` • <%= page.internet_users %> • <%= page.internet_users_rate %>
**Telephones - mobile:** <%= page.telephones_mobile %> • <%= page.telephones_mobile_subscriptions %> subs./100
EOS
## step 1: read all countries; using local json (dump) files
json_dir = '../../factbook/factbook.json'
codes = Factbook.codes.countries
pages = Factbook::JsonPageReader.new( json_dir ).read_pages( codes )
almanac = Factbook::Almanac.new( pages )
## step 2: save to disk
File.open( './ALMANAC.md', 'w:utf-8' ) do |f|
f.write almanac.render( TEMPLATE )
end
puts "Done."
(Source: factbook/script/almanac.rb)
Islamic Republic of Afghanistan › Asia -- Southern Asia, north and west of Pakistan, east of Iran
Kabul • 652,230 sq km • pop. 32,564,342 (July 2015 est.)
Languages Afghan Persian or Dari (official) 50%, Pashto (official) 35%, Turkic languages (primarily Uzbek and Turkmen) 11%, 30 minor languages (primarily Balochi and Pashai) 4%, much bilingualism, but Dari functions as the lingua franca Major cities KABUL (capital) 4.635 million (2015) Ethnic groups Pashtun, Tajik, Hazara, Uzbek, other (includes smaller numbers of Baloch, Turkmen, Nuristani, Pamiri, Arab, Gujar, Brahui, Qizilbash, Aimaq, Pashai, and Kyrghyz) Religions Sunni Muslim 80%, Shia Muslim 19%, other 1% Independence 19 August 1919 (from UK control over Afghan foreign affairs)
Internet .af
• 1.9 million • 5.9% (2014 est.)
Telephones - mobile 23.4 million • 74 (2014 est.) subs./100
Republic of Albania › Europe -- Southeastern Europe, bordering the Adriatic Sea and Ionian Sea, between Greece to the south and Montenegro and Kosovo to the north
Tirana (Tirane) • 28,748 sq km • pop. 3,029,278 (July 2015 est.)
Languages Albanian 98.8% (official - derived from Tosk dialect), Greek 0.5%, other 0.6% (including Macedonian, Roma, Vlach, Turkish, Italian, and Serbo-Croatian), unspecified 0.1% (2011 est.) Major cities TIRANA (capital) 454,000 (2015) Ethnic groups Albanian 82.6%, Greek 0.9%, other 1% (including Vlach, Roma (Gypsy), Macedonian, Montenegrin, and Egyptian), unspecified 15.5% (2011 est.) Religions Muslim 56.7%, Roman Catholic 10%, Orthodox 6.8%, atheist 2.5%, Bektashi (a Sufi order) 2.1%, other 5.7%, unspecified 16.2% Independence 28 November 1912 (from the Ottoman Empire)
Internet .al
• 1.7 million • 56.5% (2014 est.)
Telephones - mobile 3.4 million • 111 (2014 est.) subs./100
People's Democratic Republic of Algeria › Africa -- Northern Africa, bordering the Mediterranean Sea, between Morocco and Tunisia
Algiers • 2,381,741 sq km • pop. 39,542,166 (July 2015 est.)
Languages Arabic (official), French (lingua franca), Berber dialects: Kabylie Berber (Tamazight), Chaouia Berber (Tachawit), Mzab Berber, Tuareg Berber (Tamahaq) Major cities ALGIERS (capital) 2.594 million; Oran 858,000 (2015) Ethnic groups Arab-Berber 99%, European less than 1% Religions Muslim (official; predominantly Sunni) 99%, other (includes Christian and Jewish)
Independence 5 July 1962 (from France)
Internet .dz
• 6.5 million • 16.7% (2014 est.)
Telephones - mobile 37.3 million • 96 (2014 est.) subs./100
...
(Source: factbook/factbook.json/ALMANAC.md)
How about a single-file SQLite database for all factbook data?
Step 1: Create a database schema (tables and columns). Example:
CREATE TABLE "facts" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"code" varchar(255) NOT NULL,
"name" varchar(255) NOT NULL,
---------------------------------------
-- Geography
"area" integer,
"area_land" integer,
"area_water" integer,
----------------------------------------
-- People and Society
"population" integer,
"population_growth" float,
"birth_rate" float,
"death_rate" float,
"migration_rate" float
...
)
Step 2: Import the data
require 'factbook'
DB_CONFIG = {
adapter: 'sqlite3',
database: './factbook.db'
}
ActiveRecord::Base.logger = Logger.new( STDOUT )
ActiveRecord::Base.establish_connection( DB_CONFIG )
Factbook::CreateDb.new.up ## create tables
importer = Factbook::Importer.new
Factbook.codes.each do |code|
puts "Fetching #{code.code}- #{code.name}..."
page = Factbook::Page.new( code.code )
puts "Adding #{code.code}- #{code.name}..."
importer.import( page )
end
puts "Done."
Find the ten largest countries by area:
SELECT name, area FROM facts ORDER BY area DESC LIMIT 10;
Resulting in:
Russia | 17_098_242
Canada | 9_984_670
United States | 9_826_675
China | 9_596_960
Brazil | 8_515_770
Australia | 7_741_220
European Union | 4_324_782
India | 3_287_263
Argentina | 2_780_400
Kazakhstan | 2_724_900
Find the ten largest countries by population:
SELECT name, population FROM facts ORDER BY population DESC LIMIT 10;
Resulting in:
World | 7_256_490_011
China | 1_367_485_388
India | 1_251_695_584
European Union | 513_949_445
United States | 321_368_864
Indonesia | 255_993_674
Brazil | 204_259_812
Pakistan | 199_085_847
Nigeria | 181_562_056
Bangladesh | 168_957_745
And so on.