Import a file as items
Turn the records of a CSV or Excel file into items of one type, such as books or bookmarks, and import the file again later without making an item twice.
marfa items import reads a file from a spreadsheet or another app and writes each record as an item of one type. It reads CSV, TSV, JSON, JSON Lines and Excel .xlsx files. For a list you keep in columns, import it into a table instead. You need the marfa command signed in, as the Quickstart shows.
Name the type, the import and the key
An import of items takes three names:
--typeis the type of item to write, such ascore.media.book. The Catalog lists the types, and an import never makes one.--match-keyis the file's column that tells one record from another, such as the ID the other app gave each book.--asis a name for this import, such asreading-list: 1 to 64 lowercase letters, digits,.,_or-.
The name and the key are how a later import finds each item again. Marfa keeps the items under the source import:core.media.book/reading-list, with each record's key. Two files that both number their records from 1 stay apart when they have different names.
Send each column to a field
Here books.csv has the columns Book Id, Title, Author, My Rating and Shelf. A column of the file goes to the field of the type with the same name or label. A column that matches no field stops the import until you say what to do with it: --map sends it to a field, and --skip leaves it out.
marfa items import --type core.media.book --as reading-list --match-key "Book Id" \
--map "My Rating=rating" --skip Shelf books.csv --preview3 items read: 3 new, 0 changed, 0 already as the file says, 0 refused.
The file's columns: Book Id left out, Title to title, Author to author, My Rating to rating, Shelf left out.
To be added (3), with the values the file gives:
line 2 (key "1041"): title "The Leopard", author "Giuseppe Tomasi di Lampedusa", rating 5
line 3 (key "1042"): title "Middlemarch", author "George Eliot", rating 4
line 4 (key "1043"): title "The Rings of Saturn", author "W. G. Sebald"
Nothing was saved: --preview only shows what would change.--preview saves nothing.
Save the items
Run the same command without --preview. It shows the preview, asks, and saves only when you answer yes:
marfa items import --type core.media.book --as reading-list --match-key "Book Id" \
--map "My Rating=rating" --skip Shelf books.csvSave 3 new items and 0 changed items? [y/N] y
Saved 3 new items and 0 changed items; 0 items were already as the file says.
Items created (3); marfa items trash moves them to the bin:
01a2394e-2b73-7427-8be8-2cb3acb2f70f
01a2394e-2b73-7427-8be8-2cb4b6005ceb
01a2394e-2b73-7427-8be8-2cb51f7bed0dA script adds --yes to save without the question. Without a terminal, or with --json, nothing is saved unless --yes is there. A record whose value doesn't fit its field is refused alone, with its line and the reason, and the others are saved: Import and export file rules says how each kind of value is read.
Import the file again
Export the file from the other app again and run the same command, with the same --as and --match-key. Only what differs is saved:
marfa items import --type core.media.book --as reading-list --match-key "Book Id" \
--map "My Rating=rating" --skip Shelf books.csvChanges, with the value Marfa holds and the value in the file:
line 3 (key "1042")
rating: in Marfa 4, from file 5
Save 0 new items and 1 changed item? [y/N] y
Saved 0 new items and 1 changed item; 2 items were already as the file says.
Items changed (1), with the version before and after; marfa items versions ID shows the earlier ones:
01a2394e-2b73-7427-8be8-2cb4b6005ceb: version 1 to 2- A blank cell leaves the item's value as it is. Add
--blanks clearto clear it instead. - An item that someone changed after the preview is left as it is, and the report names it.
- A tag added in Marfa is kept when the file doesn't change that item's tags.
Undo an import
The report lists what to undo. Move the items it created to the bin with marfa items trash and their IDs. For an item it changed, marfa items versions <id> lists the earlier versions, and marfa items update writes back the values you want.
To get items out as a file, see Export to a spreadsheet.