# How to Handle Missing Fields in Exercise Data: JSON Schema Validation and Cleaning

> Learn to handle missing fields in exercise data. Validate against JSON schema, sanitize nulls, and use optional chaining to prevent errors. Ensure data integrity.

- Repository: [Hasan Emir Yıldırım/exercises-dataset](https://github.com/hasaneyldrm/exercises-dataset)
- Tags: how-to-guide
- Published: 2026-07-31

---

**Validate every record against the [`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json) schema before processing, sanitize null values and enum violations, and use optional chaining in TypeScript to prevent runtime crashes.**

The **hasaneyldrm/exercises-dataset** repository provides a structured collection of 1,324 exercise records, but consuming this data safely requires understanding how to handle missing fields in exercise data. The dataset enforces a strict JSON Schema that defines required fields, data types, and enumerations, making validation the first line of defense against downstream errors. When you implement a validation-first workflow, you protect mobile apps, web browsers, and analytics pipelines from `KeyError`, `undefined`, and type-mismatch crashes.

## Understanding the Schema Requirements

The dataset's integrity is governed by [`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json), which explicitly declares every required field in the `required` array (lines 39-56). This schema does **not** permit null values—there are no `type: ["string", "null"]` definitions—meaning any `null` entry constitutes a validation failure. The schema also constrains categorical data through enumerations, such as the `body_part` field which accepts only specific values like `"back"`, `"cardio"`, `"chest"`, `"lower arms"`, `"lower legs"`, `"neck"`, `"shoulders"`, `"upper arms"`, `"upper legs"`, and `"waist"` (lines 70-82).

## Three Common Missing Field Scenarios

When integrating the exercises dataset, you will encounter three distinct data quality issues that require different handling strategies.

### Absent Fields

A record missing a required key—such as `instructions` or `body_part`—fails schema validation and breaks downstream code that assumes property existence. Accessing `exercise.instructions.en` on an object without an `instructions` key throws a `KeyError` in Python or `TypeError` in JavaScript.

### Null Values Present

Even when a field key exists, a `null` value violates the schema type constraints. Since the schema specifies strict types without null unions, `null` values must be treated as validation errors requiring replacement with sensible defaults like empty strings `""` or empty arrays `[]`.

### Unexpected Types or Enum Violations

A field containing a number instead of a string, or a `body_part` value like `"arms"` (not in the allowed enum), breaks type-safe code. The schema's enumerations act as whitelists that you must validate against before processing records.

## Validation-First Workflow

Implement this three-step pipeline to robustly handle missing fields in exercise data:

1. **Load the raw JSON** from [`data/exercises.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.json).
2. **Run schema validation** using `jsonschema` (Python) or `ajv` (JavaScript).
   - If validation errors occur, decide whether to **discard** the record or **sanitize** it.
   - Sanitization involves replacing `null` with defaults and mapping unknown enum values to `"other"`.
3. **Proceed with cleaned data** where all required fields exist with correct types.

This workflow ensures that [`index.html`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/index.html) (the interactive browser) and the **LogPress** mobile app referenced in the README (lines 20-22) receive consistent data structures.

## Practical Implementation Examples

### Python: Validate and Clean with jsonschema

Use `Draft202012Validator` to check records and implement a `clean_record` function to fix nulls and invalid enums before re-validation.

```python
import json, copy
from jsonschema import Draft202012Validator

# Load schema and data

with open("data/exercises.schema.json", "r", encoding="utf-8") as f:
    schema = json.load(f)

with open("data/exercises.json", "r", encoding="utf-8") as f:
    exercises = json.load(f)

validator = Draft202012Validator(schema)

def clean_record(rec):
    """Replace nulls with defaults and map unknown enums."""
    cleaned = copy.deepcopy(rec)
    # Replace null strings with empty strings

    for key, value in cleaned.items():
        if value is None:
            if isinstance(value, str):
                cleaned[key] = ""
            elif isinstance(value, list):
                cleaned[key] = []
    # Guard body_part enum

    allowed_parts = {
        "back","cardio","chest","lower arms","lower legs",
        "neck","shoulders","upper arms","upper legs","waist"
    }
    if cleaned.get("body_part") not in allowed_parts:
        cleaned["body_part"] = "other"
    return cleaned

valid_exercises = []
for ex in exercises:
    errors = list(validator.iter_errors(ex))
    if errors:
        # Attempt to fix simple null/enum problems

        ex = clean_record(ex)
        # Re‑validate; if still bad, skip

        if not list(validator.iter_errors(ex)):
            valid_exercises.append(ex)
    else:
        valid_exercises.append(ex)

print(f"Valid records after cleaning: {len(valid_exercises)}")

```

### JavaScript: Validate with ajv and Sanitize

The `ajv` library compiles the schema into a validation function. Enable `useDefaults` to automatically populate missing fields where the schema defines defaults.

```javascript
const Ajv = require("ajv");
const fs = require("fs");

// Load schema and data
const schema = JSON.parse(fs.readFileSync("./data/exercises.schema.json"));
const exercises = JSON.parse(fs.readFileSync("./data/exercises.json", "utf8"));

const ajv = new Ajv({ allErrors: true, useDefaults: true });
const validate = ajv.compile(schema);

function sanitize(record) {
  // Replace nulls with safe defaults
  for (const [k, v] of Object.entries(record)) {
    if (v === null) {
      if (Array.isArray(record[k])) record[k] = [];
      else record[k] = "";
    }
  }
  // Guard body_part enum
  const allowed = [
    "back","cardio","chest","lower arms","lower legs",
    "neck","shoulders","upper arms","upper legs","waist"
  ];
  if (!allowed.includes(record.body_part)) record.body_part = "other";
  return record;
}

const safeExercises = [];
for (const ex of exercises) {
  if (!validate(ex)) {
    // Try a quick fix then re‑validate
    const cleaned = sanitize(ex);
    if (validate(cleaned)) safeExercises.push(cleaned);
  } else {
    safeExercises.push(ex);
  }
}

console.log(`✅ ${safeExercises.length} clean exercises ready for use`);

```

### TypeScript: Safe Access with Optional Chaining

Even after validation, use optional chaining (`?.`) and nullish coalescing (`??`) when accessing nested properties like multilingual instructions.

```typescript
import exercises from "./data/exercises.json";

exercises.forEach((ex) => {
  // `?.` prevents crashes if a language entry is unexpectedly missing
  const enInstr = ex.instructions?.en ?? "";
  console.log(`Exercise: ${ex.name} – English instructions length: ${enInstr.length}`);
});

```

## Why Validation Protects Downstream Applications

The **LogPress** mobile app consumes this dataset, and missing fields would cause immediate runtime crashes on client devices. Similarly, the [`index.html`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/index.html) interactive browser renders multilingual instruction blocks—if `instructions.en` is missing, the UI displays blank content. By enforcing [`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json) at ingestion, you ensure that [`setup.html`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/setup.html) import scripts and API integrations receive predictable data structures, eliminating defensive null checks throughout your application code.

## Summary

- **Validate first**: Always run records against [`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json) before processing to catch absent fields, null values, and type mismatches.
- **Sanitize strategically**: Replace `null` with empty strings or arrays, and map invalid `body_part` enums to `"other"` to maximize data retention.
- **Use safe access patterns**: Implement optional chaining in TypeScript and defensive programming in Python/JavaScript to handle edge cases gracefully.
- **Protect production systems**: Schema validation prevents crashes in the LogPress mobile app and the [`index.html`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/index.html) browser interface.

## Frequently Asked Questions

### What happens if I skip validation when loading exercise data?

Unvalidated data containing missing fields will trigger `KeyError` exceptions in Python or `undefined` property access errors in JavaScript when your code attempts to read required properties like `body_part` or `instructions.en`. The LogPress mobile app would crash if it received records without these mandatory fields.

### Can I modify the schema to allow null values instead of cleaning them?

While you could edit [`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json) to use union types like `type: ["string", "null"]`, this violates the dataset's design philosophy of strict typing. The repository maintains non-nullable fields to ensure consistent behavior across the [`index.html`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/index.html) browser and mobile consumers. You should sanitize data rather than relax the schema.

### How do I handle unknown body_part values not in the schema enum?

Check the `body_part` property against the allowed enumeration list defined in the schema (lines 70-82). If the value is not in the set of approved strings (`"back"`, `"cardio"`, `"chest"`, etc.), map it to `"other"` during your sanitization phase to ensure the record passes validation while preserving the data entry.

### Is the exercises dataset used in production applications?

Yes, according to the repository README (lines 20-22), this dataset powers the **LogPress** mobile application. The [`index.html`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/index.html) file also serves as a production-ready exercise browser that relies on every field being present and correctly typed, making validation critical for any integration.