Output formats
Export location data in JSON, CSV, XML, and YAML formats.
Every method that returns data supports an optional format parameter. The default format is JSON.
Supported Formats
const countries = CountryStateCity.getAllCountries('json');[
{
"id": 1,
"name": "Afghanistan",
"iso2": "AF",
"iso3": "AFG",
"phoneCode": "93",
"capital": "Kabul",
"currency": "AFN",
"emoji": "🇦🇫"
}
]Format with Filters
You can combine formats with data filtering:
// Get Turkey's states as CSV
const turkeyStatesCSV = CountryStateCity.getStatesByCountryId(225, 'csv');
// Get Istanbul's cities as XML
const istanbulCitiesXML = CountryStateCity.getCitiesByStateId(2170, 'xml');
// Export with options
const yamlData = CountryStateCity.exportData('cities', 'yaml', {
stateId: 2170,
});Using the DataFormatter
For more control over formatting, you can use the DataFormatter class directly:
import { DataFormatter } from '@tansuasici/country-state-city';
const countries = CountryStateCity.getAllCountries();
// Format a subset
const top10CSV = DataFormatter.format(countries.slice(0, 10), 'csv');
const top10XML = DataFormatter.format(countries.slice(0, 10), 'xml');