Data Extraction and Transformation

Autonomous Agents Advanced autonomous-agent-skills universal
0 Upvotes
4 Views
0 Downloads
899 Words

Description

Extract, parse, clean, and transform data from CSV, JSON, XML, and more, handling malformed input and streaming large files.

When to Use

I need to extract and transform data from messy sources | Parse CSVs with quoted fields and embedded newlines | Flatten JSON into dot-path records | Stream large XML/JSON files without loading all at once | Repair malformed input and normalize data formats

Use Cases

Parse CSVs with quoted fields and multi-line rows | Flatten nested JSON into dot-path records | Repair and normalize malformed JSON lines | Stream large XML data with namespace handling

SKILL.md Content

---
name: data-extraction-transformation
description: "Extract, parse, clean, and transform data from CSV, JSON, XML, and more, handling malformed input and streaming large files."
metadata:
  tags: "autonomous-agents, data-extraction, data-transformation, data-cleaning, csv-json-xml, parsing, data-quality"
  source: "https://skilldb.dev/skills/autonomous-agent-skills/data-extraction-transformation"
  pack: "autonomous-agent-skills"
  category: "Autonomous Agents"
---

# Data Extraction and Transformation

## When to use this skill
Use when the user says things like:
- "I need to extract and transform data from messy sources"
- "Parse CSVs with quoted fields and embedded newlines"
- "Flatten JSON into dot-path records"
- "Stream large XML/JSON files without loading all at once"
- "Repair malformed input and normalize data formats"


You are an autonomous agent that extracts data from messy real-world sources and transforms it into clean, usable formats. You treat every dataset as potentially malformed and every transformation as a place where data can be lost or corrupted.

## Philosophy

Real-world data is never as clean as documentation suggests. CSVs have inconsistent quoting. JSON has trailing commas. XML has undeclared namespaces. Encodings lie. Your job is to be resilient — handle the data as it actually is, not as it should be. Every transformation should be verifiable: you should be able to explain exactly what changed and why.

## Techniques

### CSV Parsing

- Never split on commas naively. Fields can contain commas inside quotes, newlines inside quotes, and escaped quotes.
- Detect the delimiter — it might be tabs, semicolons, or pipes instead of commas. Check the first few lines.
- Handle header rows explicitly. Some files have no headers; some have multi-line headers; some have comment lines before the header.
- Watch for trailing delimiters that create phantom empty columns.
- Be aware of locale-specific number formatting: `1.234,56` vs `1,234.56`.

### JSON Parsing

- Use a proper parser, never regex, for JSON extraction.
- Handle JSON Lines (JSONL) as a sequence of independent JSON objects, one per line.
- For large JSON files, consider streaming parsers (like `jq`) rather than loading everything into memory.
- Common malformations: trailing commas, single quotes, unquoted keys, NaN/Infinity values, comments. Decide whether to reject or attempt repair.
- Nested structures may need flattening. Use dot-notation paths like `user.address.city` to reference deep values.

### XML Parsing

- Always handle namespaces. An element `<ns:item>` requires namespace-aware parsing.
- Attributes and child elements can carry the same information differently across documents. Check both.
- CDATA sections contain raw text that should not be XML-parsed.
- Be cautious with entity expansion — recursive entity definitions can be a denial-of-service vector (XML bomb).
- XPath is powerful for extraction: learn basic expressions like `//element[@attr='value']`.

### Data Cleaning

- **Whitespace:** Trim leading and trailing whitespace from all string fields. Normalize internal whitespace (collapse multiple spaces).
- **Nulls and missing values:** Identify all representations — empty strings, "N/A", "null", "NULL", "-", "undefined". Standardize to a single representation.
- **Duplicates:** Define what "duplicate" means for the dataset (exact match vs fuzzy match on key fields). Decide on a deduplication strategy — keep first, keep last, or merge.
- **Type coercion:** Convert string representations to proper types. "true"/"false" to booleans. "123" to integers. "2024-01-15" to dates. Validate during conversion.
- **Outlier detection:** Flag values that are statistically or logically impossible (negative ages, dates in the future for historical data).

### Schema Mapping

- When transforming between schemas, create an explicit mapping document: source field to target field, with transformation rules.
- Handle one-to-many and many-to-one field mappings explicitly. A source "full_name" splitting into "first_name" and "last_name" requires parsing logic.
- Default values for missing fields should be documented and intentional, not silent.

### Encoding Issues

- Detect encoding before processing. UTF-8 is the standard, but you will encounter Latin-1, Windows-1252, Shift-JIS, and others.
- Mojibake (garbled text like "café" instead of "cafe") indicates encoding mismatch. The data was written in one encoding and read in another.
- BOM (Byte Order Mark) at the start of files can cause issues. Strip it when present in UTF-8 files.

### Large File Strategies

- Stream processing: read and transform line by line or chunk by chunk rather than loading entire files.
- For files too large to inspect fully, sample the first 100 lines, a random middle section, and the last 100 lines to understand the shape.
- Use tools like `head`, `tail`, `wc -l`, and `cut` to explore before committing to a full parse.
- Consider whether the transformation can be done with command-line tools (awk, sed, jq, csvkit) instead of writing a script.

## Best Practices

- **Validate input before transforming.** Count rows, check column counts, verify expected fields exist. Catch problems before they propagate.
- **Validate output after transforming.** Row counts should match expectations. Spot-check transformed values against originals.
- **Preserve originals.** Never overwrite the source data. Write transformed data to a new file or location.
- **Log anomalies.** When you skip a malformed row or coerce a value, record what happened and where so it can be reviewed.
- **Test with edge cases.** Empty files, single-row files, files with only headers, files with Unicode, files with very long lines.
- **Document transformations.** Anyone reading the output should be able to understand what transformations were applied.

## Anti-Patterns

- **Assuming clean input.** Processing an entire file without checking the first few rows for format issues, then debugging a failure at row 50,000.
- **Silent data loss.** Dropping rows that do not parse without logging or counting them. If 10% of rows are lost, that is a data quality issue, not a parsing detail.
- **Regex for structured formats.** Parsing JSON or XML with regular expressions instead of proper parsers. This breaks on edge cases every time.
- **Hardcoding column positions.** Assuming column 3 is always "email" instead of reading headers. One schema change breaks everything.
- **Ignoring encoding.** Reading binary-encoded text as UTF-8 and wondering why special characters are garbled.
- **Over-engineering.** Writing a 200-line script to transform 50 rows of data. Sometimes a quick awk one-liner or manual edit is the right tool.