Schema
The formal blueprint of Datarist data: how it is structured, typed, related and constrained, as JSON Schema and SQL, with the files to download.
A schema is the formal blueprint of a dataset. It defines how the data is structured (which fields, in which order), typed (what kind of value each one holds), related (which dataset points to which) and constrained (what is allowed and what is not). It is written so that a program can check data against it.
What is real here. The schemas of the sample datasets on this page are the real ones: they are published as files on the Dataset page. How Datarist will expose the schema of your own sources is placeholder content until the platform is released.
The Data dictionary explains the data to people. The schema defines it for machines. Both are generated from one definition, so they never disagree.
What a schema defines
| Aspect | What it fixes | Example |
|---|---|---|
| Structure | The fields of a row, their names and their order. | An order has nine fields, starting with order_id. |
| Types | The kind of value each field holds. | amount is a decimal. |
| Constraints | Whether a field can be empty, and its allowed values, range and pattern. | csat is from 1 to 5, or empty. |
| Keys and relationships | How a row is identified and how datasets are joined. | order_id identifies an order. customer_id points to a customer. |
| Rules | What is always true of a row, beyond one field. | users is never more than sessions. |
Types in every format
The six logical types are written the same way in every file and every system. Decimals keep a fixed number of places in CSV (249.00) and are plain numbers in JSON (249).
| Type | In CSV | In JSON | In JSON Schema | In SQL |
|---|---|---|---|---|
string | Text, quoted when it contains a comma, a quote or a line break | string | string | TEXT |
integer | Digits, no decimal point | number | integer | INTEGER |
decimal | Digits with a fixed number of decimal places | number | number | NUMERIC(12, n) |
boolean | true or false | boolean | boolean | BOOLEAN |
date | YYYY-MM-DD | string | string, format date | DATE |
datetime | ISO 8601 in UTC, ending in Z | string | string, format date-time | TIMESTAMPTZ |
Constraints
Limits on a field
| Limit | JSON Schema | SQL | Example |
|---|---|---|---|
| Cannot be empty | The type has no null; the field is required | NOT NULL | order_id |
| Fixed set of values | enum | CHECK (… IN (…)) | status |
| Smallest and largest value | minimum, maximum | CHECK (… BETWEEN … AND …) | csat, from 1 to 5 |
| Shape of a text value | pattern | CHECK (… ~ '…') | customer_id, like C00042 |
Keys
| Dataset | Primary key |
|---|---|
| Customers | customer_id |
| Orders | order_id |
| Web traffic | date with page_path |
| Support tickets | ticket_id |
Rules
JSON Schema cannot express a rule that compares two fields, so the JSON files carry the rules in x-rules for readers, and the SQL enforces every one as a CHECK. The rules of each dataset are listed in the Data dictionary.
Relationships
Every foreign key is a relationship in which many rows here point to one row there. A foreign key is enforced: a value that does not exist in the dataset it points to is invalid.
| From | To | Kind | Meaning |
|---|---|---|---|
orders.customer_id | customers.customer_id | many to one | Each row of Orders belongs to one row of Customers. |
support_tickets.customer_id | customers.customer_id | many to one | Each row of Support tickets belongs to one row of Customers. |
customers 1 ---< many orders
customers 1 ---< many support_tickets
web_traffic: no relationshipsJSON Schema
Each dataset has a JSON Schema (draft 2020-12) in <name>.schema.json. It describes the JSON file of the dataset: an array of rows, every row with every field, no extra fields. A field that can be empty is null in the row. This is the Orders schema, shortened to three fields:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Orders",
"description": "One row per order: amount, currency, sales channel and payment status. Join to Customers on customer_id.",
"type": "array",
"items": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "Unique order identifier. Primary key.",
"pattern": "^O\\d{6}$"
},
"customer_id": {
"type": "string",
"description": "The customer who placed the order. Foreign key to Customers.customer_id.",
"pattern": "^C\\d{5}$"
},
"discount_pct": {
"type": [
"integer",
"null"
],
"description": "Discount applied, in whole percent. Empty when none.",
"minimum": 0,
"maximum": 100
}
},
"required": [
"order_id",
"customer_id",
"discount_pct"
],
"additionalProperties": false,
"x-primaryKey": [
"order_id"
],
"x-foreignKeys": [
{
"field": "customer_id",
"references": "customers.customer_id"
}
],
"x-rules": [
{
"id": "paid-has-timestamp",
"description": "paid_at is filled exactly when status is paid or refunded."
},
{
"id": "paid-not-before-order",
"description": "paid_at is not before order_date."
}
]
}
}Check a file against its schema with any JSON Schema validator. In Python, with the jsonschema package:
import json
from jsonschema import Draft202012Validator
schema = json.load(open("orders.schema.json"))
rows = json.load(open("orders.json"))
Draft202012Validator(schema).validate(rows) # raises if any row breaks the schemaSQL
The same blueprint as tables. It uses standard SQL types and CHECK constraints, written for PostgreSQL. Create the tables in this order, because Orders and Support tickets point to Customers.
CREATE TABLE customers (
customer_id TEXT NOT NULL CHECK (customer_id ~ '^C\d{5}$'),
signup_date DATE NOT NULL,
country TEXT NOT NULL CHECK (country IN ('KE', 'US', 'GB', 'DE', 'FR', 'NG', 'ZA', 'IN', 'BR', 'CA')),
plan TEXT NOT NULL CHECK (plan IN ('free', 'team', 'business', 'enterprise')),
segment TEXT NOT NULL CHECK (segment IN ('individual', 'smb', 'mid-market', 'enterprise')),
seats INTEGER NOT NULL CHECK (seats >= 0),
is_active BOOLEAN NOT NULL,
churned_at DATE,
PRIMARY KEY (customer_id),
CONSTRAINT customers_churn_matches_active CHECK (is_active = (churned_at IS NULL)),
CONSTRAINT customers_free_has_no_seats CHECK ((plan = 'free') = (seats = 0)),
CONSTRAINT customers_churn_after_signup CHECK (churned_at IS NULL OR churned_at >= signup_date)
);
CREATE TABLE web_traffic (
date DATE NOT NULL,
page_path TEXT NOT NULL CHECK (page_path IN ('/', '/data', '/docs', '/insight', '/about', '/contact')),
sessions INTEGER NOT NULL CHECK (sessions >= 0),
users INTEGER NOT NULL CHECK (users >= 0),
pageviews INTEGER NOT NULL CHECK (pageviews >= 0),
avg_engagement_seconds NUMERIC(12, 1) NOT NULL CHECK (avg_engagement_seconds >= 0),
bounce_rate NUMERIC(12, 3) NOT NULL CHECK (bounce_rate BETWEEN 0 AND 1),
PRIMARY KEY (date, page_path),
CONSTRAINT web_traffic_users_within_sessions CHECK (users <= sessions),
CONSTRAINT web_traffic_pageviews_cover_sessions CHECK (pageviews >= sessions)
);
CREATE TABLE orders (
order_id TEXT NOT NULL CHECK (order_id ~ '^O\d{6}$'),
customer_id TEXT NOT NULL CHECK (customer_id ~ '^C\d{5}$'),
order_date DATE NOT NULL,
channel TEXT NOT NULL CHECK (channel IN ('web', 'sales', 'partner', 'marketplace')),
currency TEXT NOT NULL CHECK (currency IN ('USD', 'EUR', 'GBP', 'KES')),
amount NUMERIC(12, 2) NOT NULL CHECK (amount >= 0),
discount_pct INTEGER CHECK (discount_pct BETWEEN 0 AND 100),
status TEXT NOT NULL CHECK (status IN ('paid', 'pending', 'refunded', 'failed')),
paid_at TIMESTAMPTZ,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT orders_paid_has_timestamp CHECK ((status IN ('paid', 'refunded')) = (paid_at IS NOT NULL)),
CONSTRAINT orders_paid_not_before_order CHECK (paid_at IS NULL OR CAST(paid_at AS DATE) >= order_date)
);
CREATE TABLE support_tickets (
ticket_id TEXT NOT NULL CHECK (ticket_id ~ '^T\d{6}$'),
created_at TIMESTAMPTZ NOT NULL,
customer_id TEXT NOT NULL CHECK (customer_id ~ '^C\d{5}$'),
category TEXT NOT NULL CHECK (category IN ('billing', 'access', 'data-quality', 'integrations', 'how-to')),
priority TEXT NOT NULL CHECK (priority IN ('low', 'normal', 'high', 'urgent')),
status TEXT NOT NULL CHECK (status IN ('open', 'pending', 'solved')),
first_response_minutes INTEGER CHECK (first_response_minutes >= 0),
resolution_hours NUMERIC(12, 1) CHECK (resolution_hours >= 0),
csat INTEGER CHECK (csat BETWEEN 1 AND 5),
PRIMARY KEY (ticket_id),
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT support_tickets_solved_has_resolution CHECK ((status = 'solved') = (resolution_hours IS NOT NULL)),
CONSTRAINT support_tickets_csat_only_when_solved CHECK (csat IS NULL OR status = 'solved'),
CONSTRAINT support_tickets_resolution_after_response CHECK (resolution_hours IS NULL OR first_response_minutes IS NULL OR resolution_hours >= first_response_minutes / 60.0)
);Then load each CSV file. Empty cells become NULL.
psql -c "\copy customers FROM 'customers.csv' WITH (FORMAT csv, HEADER true)"
psql -c "\copy web_traffic FROM 'web-traffic.csv' WITH (FORMAT csv, HEADER true)"
psql -c "\copy orders FROM 'orders.csv' WITH (FORMAT csv, HEADER true)"
psql -c "\copy support_tickets FROM 'support-tickets.csv' WITH (FORMAT csv, HEADER true)"Where the files are
| File | What it is |
|---|---|
<name>.schema.json | The JSON Schema of the dataset. |
<name>.dictionary.json, <name>.dictionary.csv | The data dictionary of the dataset. |
catalog.json | Every dataset with its fields, files, sizes and SHA-256 checksums. |
datarist-datasets-csv.zip, datarist-datasets-json.zip | Everything above, in one download each, with a README. |
All of them are on the Dataset page. Through the API, dataset schemas returns the schema of a dataset.
Changing a schema
- Compatible changes add something: a new dataset, or a new field that can be empty. Code that reads the old schema keeps working.
- Breaking changes take something away or tighten it: removing or renaming a field, changing a type, narrowing the allowed values. They are announced in the Changelog and marked as breaking.
- The datasets carry an Updated date. Cite that date, and the file’s SHA-256 checksum, when you need to point to exactly one version. See Citation.