farling42 / obsidian-import-json

Plug-in for Obsidian.md which will create Notes from JSON files
MIT License
85 stars 5 forks source link

Problems with importing from json file #54

Closed ReaderGuy42 closed 11 months ago

ReaderGuy42 commented 11 months ago

I have json files generated from a database program, e.g.

{

  "tags": [
    "Action & Adventure",
    "War & Military",
    "Genre Fiction",
    "Fantasy",
    "Epic",
    "Historical"
  ],
 "metadata": {
    "title": "The Blade Itself",
    "subtitle": null,
    "authors": [
      "Joe Abercrombie"
    ],
    "narrators": [
      "Steven Pacey"
    ],
    "series": [
      "First Law #1"
    ],
    "genres": [
      "Literature & Fiction",
      "Science Fiction & Fantasy"
    ],
    "publishedYear": "2015",
    "publishedDate": null,
    "publisher": "Hachette Audio",
    "description": "long text ",
  }
}

I can query metadata specifically by adding metadata to that field in your plugin, however, then I can't get other areas, like tags, or genres.

[On that handlebars website it works](https://handlebarsjs.com/playground.html#format=1&currentExample=%7B%22template%22%3A%22%7B%7Bmetadata.title%7D%7D%20by%20%7B%7Bmetadata.authors%7D%7D%5Cn%5Cn%7B%7Btags%7D%7D%5Cn%5CnInfo%3A%20%7B%7Bmetadata.description%7D%7D%5Cn%5Cn%5Cn%22%2C%22partials%22%3A%5B%5D%2C%22input%22%3A%22%7B%5Cn%20%20%5Cn%20%20%5C%22tags%5C%22%3A%20%5B%5Cn%20%20%20%20%5C%22Action%20%26%20Adventure%5C%22%2C%5Cn%20%20%20%20%5C%22War%20%26%20Military%5C%22%2C%5Cn%20%20%20%20%5C%22Genre%20Fiction%5C%22%2C%5Cn%20%20%20%20%5C%22Fantasy%5C%22%2C%5Cn%20%20%20%20%5C%22Epic%5C%22%2C%5Cn%20%20%20%20%5C%22Historical%5C%22%5Cn%20%20%5D%2C%5Cn%20%5C%22metadata%5C%22%3A%20%7B%5Cn%20%20%20%20%5C%22title%5C%22%3A%20%5C%22The%20Blade%20Itself%5C%22%2C%5Cn%20%20%20%20%5C%22subtitle%5C%22%3A%20null%2C%5Cn%20%20%20%20%5C%22authors%5C%22%3A%20%5B%5Cn%20%20%20%20%20%20%5C%22Joe%20Abercrombie%5C%22%5Cn%20%20%20%20%5D%2C%5Cn%20%20%20%20%5C%22narrators%5C%22%3A%20%5B%5Cn%20%20%20%20%20%20%5C%22Steven%20Pacey%5C%22%5Cn%20%20%20%20%5D%2C%5Cn%20%20%20%20%5C%22series%5C%22%3A%20%5B%5Cn%20%20%20%20%20%20%5C%22First%20Law%20%231%5C%22%5Cn%20%20%20%20%5D%2C%5Cn%20%20%20%20%5C%22genres%5C%22%3A%20%5B%5Cn%20%20%20%20%20%20%5C%22Literature%20%26%20Fiction%5C%22%2C%5Cn%20%20%20%20%20%20%5C%22Science%20Fiction%20%26%20Fantasy%5C%22%5Cn%20%20%20%20%5D%2C%5Cn%20%20%20%20%5C%22publishedYear%5C%22%3A%20%5C%222015%5C%22%2C%5Cn%20%20%20%20%5C%22publishedDate%5C%22%3A%20null%2C%5Cn%20%20%20%20%5C%22publisher%5C%22%3A%20%5C%22Hachette%20Audio%5C%22%2C%5Cn%20%20%20%20%5C%22description%5C%22%3A%20%5C%22Long%20text%20description%20%5C%22%2C%5Cn%20%20%20%20%5C%22isbn%5C%22%3A%20null%2C%5Cn%20%20%20%20%5C%22asin%5C%22%3A%20%5C%22B014LL6R5U%5C%22%2C%5Cn%20%20%20%20%5C%22language%5C%22%3A%20%5C%22English%5C%22%2C%5Cn%20%20%20%20%5C%22explicit%5C%22%3A%20false%2C%5Cn%20%20%20%20%5C%22abridged%5C%22%3A%20false%5Cn%20%20%7D%5Cn%7D%5Cn%22%2C%22output%22%3A%22The%20Blade%20Itself%20by%20Joe%20Abercrombie%5Cn%5CnAction%20%26amp%3B%20Adventure%2CWar%20%26amp%3B%20Military%2CGenre%20Fiction%2CFantasy%2CEpic%2CHistorical%5Cn%5CnInfo%3A%20Long%20text%20description%20%5Cn%5Cn%5Cn%22%2C%22preparationScript%22%3A%22%2F%2F%20Handlebars.registerHelper('loud'%2C%20function(string)%20%7B%5Cn%2F%2F%20%20%20%20return%20string.toUpperCase()%5Cn%2F%2F%20%7D)%3B%5Cn%22%2C%22handlebarsVersion%22%3A%224.7.8%22%7D)

However, if I put the same thing in the template, it doesn't do anything.

farling42 commented 11 months ago

You will need to process this JSON file in two separate passes: once to process the metadata and once to process the array of data in the tags.

If you are creating a single note from all the data, then you don't need to specify a top-level name at all (so that the entire object is used), in which case your handlebars template would need to specify {{metadata.title}} and {{#each tags}}... to process the information into a single note.

ReaderGuy42 commented 11 months ago

OK, I currently have this as my handlebar template:

---
Title: "{{metadata.title}}"
Authors: "{{metadata.authors}}"
Genres: "{{#each genres}} {{this}}{{#unless @last}},{{/unless}}{{/each}}"
tags: "{{#each tags}} {{this}}{{#unless @last}},{{/unless}}{{/each}}"
Description: "{{metadata.description}}"
---

This works for everything except the genres, they just don't show up at all. This seems odd since they appear to be handled the same as tags?

farling42 commented 11 months ago

tags is at the top level of your JSON object.

genres is a field within the metadata field of your JSON object. So it needs to accessed using {{#each metadata.genres}}

ReaderGuy42 commented 11 months ago

You're right, I saw that wrong, I thought genre was also top level. Thanks!!