How to Add Computed Fields to JSON or CSV

You have an order list and need a total column. Or user records where you want a lowercase username. The usual answers are a throwaway Python script or a spreadsheet round-trip. There's a shorter path.

The Transmute CLI (free)

The add operation computes new fields from existing ones. Given orders.json:

[
  { "name": "Alice", "price": 10, "qty": 2 },
  { "name": "Bob",   "price": 5,  "qty": 4 }
]
$ transmute orders.json \
    --pipe '[{"op":"add","fields":{"total":"item.price * item.qty"}}]' \
    --output json
[
  {
  "name": "Alice",
  "price": 10,
  "qty": 2,
  "total": 20
  },
  {
  "name": "Bob",
  "price": 5,
  "qty": 4,
  "total": 20
  }
]

add works on CSV input too — the types are coerced first, so arithmetic just works:

$ transmute orders.csv \
    --pipe '[{"op":"add","fields":{"total":"item.price * item.qty"}}]' -o csv

Combine with the other pipeline steps — here adding a field and filtering in one pass:

$ transmute orders.json \
    --pipe '[{"op":"add","fields":{"total":"item.price * item.qty"}},{"op":"filter","expr":"item.total >= 20"}]' \
    --output json

The alternatives

  • jq: jq '. + {total: (.price * .qty)}' works, but jq's syntax is its own language — great once learned, easy to forget between uses.
  • Python: a dict comprehension per row plus file I/O. Fine for a one-off, but it's a script you'll never commit and rewrite next month.
  • Spreadsheet: import, formula, export — and hope the number formatting survives the trip.

Gotchas

  • CSV types are inferred. "007" stays a string on purpose (IDs, zip codes); "7" becomes a number. Check edge-case rows once.
  • A failing expression yields null instead of crashing the whole run — handy for messy data, but scan for nulls afterwards ({"op":"filter","expr":"item.total === null"}).