Lesson

Structuring Complex Data

We've touched on JSON and tables, but let's dive deeper into how Claude handles complex data extraction. One of the most painful tasks in software development is writing custom parsing logic for messy, unstructured data.

Imagine you receive a weekly email from a client that contains a list of their new employees. Sometimes it's a bulleted list, sometimes it's a paragraph, sometimes they forget the last name. Writing a regex to parse that reliably is a nightmare.

Claude can do it instantly.

The Power of the Schema

If you want Claude to extract data reliably, you shouldn't just ask for JSON. You should provide a schema or an interface.

Prompt:

"Extract the employee data from the following text.

You MUST format the output as a JSON array of objects that strictly adheres to this TypeScript interface:

interface Employee {
  firstName: string;
  lastName: string | null;
  role: 'Engineer' | 'Designer' | 'Manager' | 'Unknown';
  salaryEstimate: number;
}

If a last name is not provided, use null. If the role does not exactly match one of the enum values, use 'Unknown'. Do not output anything except the JSON."

By giving Claude the exact TypeScript interface, you are eliminating the guesswork. It knows exactly what keys to use, and it knows exactly what to do if data is missing.

Fixing Bad Data

Claude isn't just a parser; it can sanitize data on the fly.

If your client sends over phone numbers in wildly different formats (e.g., (555) 123-4567, 555.123.4567, 555 123 4567), you can add a sanitation rule directly in the prompt.

"For the phoneNumber field, format all extracted phone numbers to standard E.164 format (e.g., +15551234567). If a number is invalid, set it to null."

Using XML Tags for Complex Prompts

When you start dealing with complex data parsing, your prompts will get long. The best way to organize a long prompt is by using XML tags. Claude is specifically trained to recognize XML tags as boundaries for information.

Prompt:

"Extract data from the text inside the <input_text> tags. Format it according to the schema inside the <schema> tags.

<schema> { "name": string, "age": number } </schema>

<input_text> ... long messy text here ... </input_text>"

Using tags prevents Claude from getting confused between your instructions and the actual data you want it to process.

Ready to test your understanding? Take the quiz to reinforce what you learned.

© 2026 Ant Skillsv.26.07.07-23:01
Ant Skills - Lesson