# Edit Fields (Set) and Expressions — AI Automation with n8n

Source: https://www.geekswithgeeks.com/en/n8n-ai-automation/data-set-node

> Create and rename fields with the Edit Fields node and write safe expressions.

## Build the exact payload

The **Edit Fields** (Set) node creates, renames or removes fields so the next system receives exactly the shape it expects. Use it before API calls to build the request body and after them to keep only the fields you need, which also keeps execution data small. Guard against missing values with `?.` and `??` in expressions.

## Shape the data

Most automation work is cleaning, reshaping and checking data between two systems.

![Four steps: clean, validate, reshape, batch.](assets/figures/n8n-ai-automation/section-3-map.svg) — Figure 3.1 — Clean, validate, reshape and batch.

## Safe expressions

Without `?.`, reading a nested field of a missing object throws an error and stops the item. `??` supplies a default.

```text
full_name : {{ $json.first_name }} {{ $json.last_name }}
city      : {{ $json.address?.city ?? "Unknown" }}
total_inr : {{ Number($json.total) || 0 }}
is_big    : {{ $json.total > 5000 }}
```

## Keep only what you need

Dropping unused fields early makes later nodes simpler and avoids sending personal data to services that do not need it.

**Quiz:** What does `$json.address?.city ?? "Unknown"` do when address is missing?

- [ ] Throws an error
- [ ] Deletes the item
- [x] Returns "Unknown"
- [ ] Returns the address object

*Answer:* Returns "Unknown". Optional chaining avoids the error, and the nullish default fills in the fallback value.
