Published

February 13, 2025

Key-value properties

Background on key-value properties

Obsidian lets you add metadata, composed of key-value pairs, to any note in a YAML file header. In addition, with the dataview plugin, you can include key-value properties in in-line fields (hidden or visible) in your notes.

Start from an Obsidian vault

We’ll use the small test vault in the test/data/presidents-vault directory of the package’s github repository, so if you want to see what the source notes look like, explore the vault there:

using Obsidian
vaultdir = joinpath(root, "test", "data", "presidents-vault")
v = Vault(vaultdir)
"Obsidian vault with 12 notes"

Find properties for a note

To find properties for a single note, use the kvpairs function.

lincolntags = kvpairs(v,"Abraham Lincoln 1860 census")
12-element Vector{Any}:
 (k = "Refersto", v = "[[Abraham Lincoln]]")
 (k = "Enumeration", v = "[[Springfield, Sangamon, Illinois]]")
 (k = "Date", v = "1860-07-14")
 (k = "House", v = "1002")
 (k = "Family", v = "989")
 (k = "Name", v = "Abraham Lincoln")
 (k = "Age", v = "51")
 (k = "Sex", v = "M")
 (k = "Occupation", v = "Sawyer")
 (k = "Real estate value", v = "5000")
 (k = "Personal estate value", v = "12000")
 (k = "Birthplace", v = "Ill.")

The result is a simple list of named tuples, so you can use normal Julia mapping and filtering to work with it. For example, use map to find the data categories recorded for Abraham Lincoln in the 1860 census:

map(tag -> tag.k, lincolntags)
12-element Vector{String}:
 "Refersto"
 "Enumeration"
 "Date"
 "House"
 "Family"
 "Name"
 "Age"
 "Sex"
 "Occupation"
 "Real estate value"
 "Personal estate value"
 "Birthplace"

Use filter and map in succession to find the date when the census record was taken:

datetags = filter(tag -> tag.k == "Date", lincolntags)
map(tag -> tag.v, datetags)
1-element Vector{String}:
 "1860-07-14"

Note that the Refersto property returns a wikilink.

refersto = filter(tag -> tag.k == "Refersto", lincolntags)
map(tag -> tag.v, refersto)
1-element Vector{String}:
 "[[Abraham Lincoln]]"

Work with properties for an entire vault

The kvtriples finds all properties in a vault. It returns a list of NoteKV objects with three values: the name of the wiki page, the key and the value, accessible with the wikiname, key and value functions, respectively. Find the unique set of notes that have properties:

allkvtags = kvtriples(v)
wikiname.(allkvtags) |> unique
4-element Vector{String}:
 "Springfield, Sangamon, Illinois"
 "Abraham Lincoln 1860 census"
 "Abraham Lincoln"
 "yamltest"
  • find pages and values for key
  • find pages for key-value pair