Dataview: how to query for frontmatter keys?

Hi,

I use the following query:

```dataview
Table file.frontmatter
Where contains(note_type, "source")
```

which creates a table of all files, that contain the key-value-pair “note_type::source” and show their frontmatter.

My aim is to create a similar table, but not with the whole frontmatter, but only the key-part of the key-value-pairs…
I tried

```dataview
Table file.frontmatter.key
Where contains(note_type, "source")
```

or

```dataview
Table file.frontmatter[0]
Where contains(note_type, "source")
```

but that didn’t work.

Do you have a hint for me?

Thanks in advance,
kind regards,

Silias

I don’t think the standard DQL queries are able to extract the keys of an object, so you need to switch to dataviewjs if I’m not mistaken. Then you’re able to use Object.keys() to extract the keys of an object like file.frontmatter (or the equivalent).

Here are two queries doing just that:

```dataviewjs
const frontmatter = dv.current().file.frontmatter

console.log(Object.keys(frontmatter))
```

## Query variant

```dataviewjs
const result = await dv.pages()
  .where(p => p.note_type == "source")
  .map(p => [ p.file.name, Object.keys(p.file.frontmatter) ] )

if (result.successful) {
 dv.table(["File name", "Keys"], result) 
} else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```

Thanks @holroy! Too bad, that it’s not possible with normal dataview, as I don’t understand java script too much…
Your second script works, it produces a table with two columns:

  1. column: all files, that have note_type = “source”
  2. column: all the frontmatter-keys of each file

… could you please help me, to modify that script, so that it will generate a list instead of table, which shows the consolidated frontmatter-keys, which are in the files. => each frontmatter-key should be listed just once, even though it occurs in more than one file.

… I already asked ChatGPT for that, but didn’t get a reasonable answer… :wink:

Thanks in advance,
kind regards,

Silias

Have you tried turning on the core plugin of Properties view, and executing the Properties: Show all properties command? This’ll list all frontmatter keys, with a count of how many times it’s used, if I’m not mistaken (on mobile now).

Hi holroy,
thanks for your answer!
Yeah, I know this feature. As I recently changed my PKM system (no “note_type:: source” properties anymore, but #source tags instead) I don’t need such a Table anymore.
Cheers,
Silias

I’ve been a little back and forth, but currently I’m using note_type: ??? style, as it’s easier to query, and I like the look of it, and my personal opinion is that it doesn’t clutter up the tag cloud as much. But to each their own.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.