# Structured Output and Extraction — AI Automation with n8n

Source: https://www.geekswithgeeks.com/en/n8n-ai-automation/ai-structured-output

> Get reliable JSON from a model using the Information Extractor or an output parser.

## Make the model fill a form

Free-text answers are hard to use in later nodes. The **Information Extractor** node takes text and a list of fields you want (name, date, amount) and returns them as structured data. An **output parser** with a JSON schema on an LLM chain does the same job. Always validate the result, because the model may return null for missing fields or misread a number.

## A schema for an invoice

Descriptions on each property guide the model. Dates in ISO format are easy for later nodes to parse.

```json
{
  "type": "object",
  "properties": {
    "vendor": { "type": "string", "description": "Company that issued the invoice" },
    "date":   { "type": "string", "description": "Invoice date in YYYY-MM-DD" },
    "total":  { "type": "number", "description": "Total amount payable, numeric only" }
  },
  "required": ["vendor", "total"]
}
```

## Validate in a Code node

After extraction, add a small Code or IF node that checks the total is a positive number and the date parses. Route failures to a human-review path instead of posting bad data to your accounting system.

**Quiz:** Why validate data after an Information Extractor step?

- [ ] Extractors never work
- [x] The model might misread values or return null
- [ ] Validation makes data longer
- [ ] n8n deletes unvalidated data

*Answer:* The model might misread values or return null. A well-formed JSON object can still contain a wrong number or missing value.
