Once you’ve navigated to a specific table, you need to construct a query that tells the API exactly what data you want. Queries are sent as JSON objects and specify which variables to include, what filters to apply, and what format you want the response in.
When you navigated to the table, you saw the available variables:
[1] "WaterDisposal" "Locality" "Geographic_Area"
These are the variables you can filter and select in your query.
2
Define Your Query List
Create a query to select specific water disposal methods:
query_list = list( query = list( list( code = "WaterDisposal", selection = list( filter = "item", values = list( "Total households in occupied dwelling unit", "Through the sewerage system") # Selecting one specific method ) ), # We must select values for other variables or the API might aggregate them list( code = "Geographic_Area", selection = list(filter = "all", values = list("*")) ) ), response = list( format = "csv" # We want the data back as a CSV ))
3
Understand the Query Components
Let’s break down each part:Variable Selection:
list( code = "WaterDisposal", # The variable name selection = list( filter = "item", # Filter type values = list(...) # Specific values to include ))
All Variables Required: You must include all table variables in your query. If a table has three variables, your query must specify selections for all three.
Exact Value Names: Variable values are case-sensitive and must match exactly as returned by the metadata. “Through the sewerage system” is different from “through the sewerage system”.
Not sure what values to use in your query? See the Working with Metadata guide to learn how to inspect table metadata and find all valid values for each variable.
Now that you’ve constructed a query, you’re ready to retrieve and parse the data.See the Retrieving Data guide to learn how to execute your query and process the results.