Querying complex data types from metadata using dataview

Hi all, I’m a new Obsidian user. I’m stuck with something that seems pretty basic, but unfortunately I was not able to find out the answer…

Consider this metadata YAML block:

---
cast:
  characterA: actor1
  characterB: actor2
---

What would be the right way of showing a table with 2 columns: “Character Name” and “Actor Name”?

My intuition was to try to somehow select FROM the cast field, but I was not able to do it.
All I am able to obtain is a table such as this:


cast(1)

  • characterA: actor1
  • characterB: actor2

Which I guess is not terrible, but not really what I wanted.

Any tips? Thanks in advance,

Rogério.

1 Like

By the way, the query I used was simply this:

TABLE WITHOUT ID
cast
WHERE file.name = this.file.name
```dataview
TABLE WITHOUT ID cast.characterA, cast.characterB
WHERE file.name = this.file.name
```

Something like this?

1 Like

Does this work?

```dataviewjs
// Assigns the page's cast entry to cast
const cast = dv.current().cast

const array = []
// Pushes each key and value pair to array
for (const [key, value] of Object.entries(cast)) {
	array.push([key, value])
}
// Render table with headers and array as data
dv.table(["Character Name", "Actor Name"], array)
2 Likes

I totally misread the request, and there isn’t currently a proper way to get the keys of an object using a DQL query. I just read your request related to how to get to the fields within the object, cast. Sorry for misunderstanding.

You’ll need to use a query like the one provided here by @AlexTheSleepy , or you need to retype your keys, and do something like:

```dataview 
TABLE WITHOUT ID character as "Character name", cast[character] as "Actor name"
FLATTEN list(
  "characterA",
  "characterB" ) as character
WHERE file.path = this.file.path
```

Which also would produce:

image

1 Like

Thanks a lot, both @holroy and @AlexTheSleepy!

Yes, Alex’s answer is indeed the one I was looking for… I was 99% sure dataviewjs was the way to go, but I did want to know if a “pure” DQL query would be possible.

My vanilla/frontend JavaScript knowledge is not that bad, but dataviewjs is still very unknown to me, will definitely dig deeper into it.

1 Like

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