Overview
The GSS StatsBank API supports multiple output formats for retrieved data. The format is specified in theresponse component of your query using the format parameter.
Specifying Format
The format is defined in the query list:Available Formats
The GSS StatsBank API, built on PxWeb technology, supports multiple formats. Refer to the PxWeb API documentation for the complete list.CSV Format
Returns data as comma-separated values.
- Human-readable tabular format
- Easy to import into spreadsheet applications
- Compatible with
readr::read_csv()in R - Typically returns data in “wide” format with regions as columns
JSON Format
Returns data as standard JSON.
- Structured hierarchical data
- Easy to parse programmatically
- Good for web applications and APIs
- Compatible with
resp_body_json()in R
JSON-stat Format
Returns data in JSON-stat format (version 1.x).
- Standardized format for statistical data
- Includes metadata alongside data values
- Supported by statistical software and libraries
- More verbose than plain JSON
JSON-stat2 Format
Returns data in JSON-stat format (version 2.x).
- Updated version of JSON-stat
- Improved efficiency and structure
- Better support for modern statistical applications
Format Selection Guidelines
Use CSV When:
- Working with R and tidyverse tools
- Need to quickly view data in Excel or Google Sheets
- Performing data analysis and visualization
- Want a simple, straightforward format
Use JSON When:
- Building web applications
- Need programmatic access to nested data
- Integrating with JavaScript applications
- Prefer object-oriented data structures
Use JSON-stat/JSON-stat2 When:
- Working with statistical software that supports the format
- Need rich metadata alongside data
- Building statistical applications
- Require standardized statistical data exchange
Complete Examples
CSV Example
JSON Example
Processing CSV Data
CSV responses from the API typically come in “wide” format, which may require reshaping:Wide Format Example
Raw CSV from API:Tidy Format After Transformation
Afterpivot_longer and pivot_wider:
Response Body Methods
For CSV Format
Useresp_body_raw() to get raw bytes, then parse with read_csv():
For JSON Formats
Useresp_body_json() to automatically parse JSON:
For Other Formats
Useresp_body_string() to get the raw text:
Additional Format Options
The PxWeb API may support additional formats depending on the configuration. Consult the PxWeb API documentation for:- Excel formats (XLSX)
- SDMX formats
- XML formats
- Custom formats specific to the installation
Error Handling
Invalid Format
If an unsupported format is specified, the API will return an error:Format Not Specified
If theformat parameter is omitted, the API’s default format will be used (typically JSON).
Performance Considerations
CSV
- Generally smaller file size than JSON
- Faster parsing in R with
readr - Best for large datasets
JSON
- Larger file size due to structure overhead
- Slower parsing for very large datasets
- Better for nested/hierarchical data
JSON-stat
- Largest file size due to metadata inclusion
- Slowest parsing
- Best when metadata is critical
Recommended Workflow
For most R-based data analysis projects:- Use CSV format for data retrieval
- Parse with
readr::read_csv()for fast, type-safe loading - Transform with
tidyrto reshape data as needed - Analyze with tidyverse tools or base R