farling42 / obsidian-import-json

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

How to access the nested input objects? #19

Closed seed78 closed 2 years ago

seed78 commented 2 years ago

Hi, Thanks for developing such a nice plugin. I have a question about accessing the nested input objects in JSON file. I want to import "path" into a Obsidian template.

I have tired {{attachments.path}}. But it doesn't work. Could you please tell me how to import "path" correctly?

Thanks!

  "attachments": [
    {
      "dateAdded": "2022-06-25T16:22:45Z",
      "dateModified": "2022-06-25T16:23:05Z",
      "itemType": "attachment",
      "path": "C:\\folder\\file.pdf",
      "relations": [],
      "tags": [],
      "title": "file.pdf",
      "uri": "http://zotero.org/users/8045138/items/BSI5IT99"
    }
farling42 commented 2 years ago

For you JSON, the importer will detect the array as being inside the "attachment" field. So the markdown template acts as if there is no parent fields above the individual array element. 

This means you can access attachments[0].path simply as "path" 

seed78 commented 2 years ago

Thanks for the response. I tried either attachments[0].path or path. They didn't work. I attached the complete json file that I exported from Zotero with the "BetterBibTex JSON" format . I tried to move "path" to the same nested level as "DOI" and I used {{path}} and it can work. However, when "path" is nested within "attachments", the same {{path}} doesn't work. Could you please let me know how I can import {{path}} correctly?

Thank you!

test.zip .

farling42 commented 2 years ago

If you selecting "items" as the top-level array from which to generate notes, then since attachments is an array, you probably want to use {{#each attachments}} block helper to be able to access information about each element in the attachment array.

seed78 commented 2 years ago

Hi, I am not an experienced code writer. I simply wrote {{#each attachments}} in an Obsidian template. It doesn't work either. I looked up the webpage of "Block Helper". It said "These block helpers are identified by a # preceeding the helper name and require a matching closing mustache, /, of the same name." This seems for HTML. Could you please use the test.json (in the attached test.zip file in my previous post) as the example and demonstrate the code to access "path" in the JSON file? I have no idea how to write a working code.

Thanks a lot in advance.

farling42 commented 2 years ago

Using your test.json, I put the following information into the "Import JSON/CSV dialog"

Choose JSON/CSV file: test.json Choose TEMPLATE file: test.md (see below) Field containing the data: items Field to use as Note name: publicationTitle Allow paths in Note name: ticked Name of Destination Folder in Vault: books

The test.md contained the following

Title: {{publicationTitle}}
Date: {{date}}

Path is:

{{#each attachments}}
- {{path}}
{{/each}}
farling42 commented 2 years ago

"Field containing the data" points to the top-level array in the JSON file to be decoded.

"{{#each attachments}} ... {{/each}}" loops through the entries in the array called attachments that is within each entry in the array.

"{{path}}" is the name of a field within the individual element of the attachments array which is being processed on this step through the attachments array loop.

"- " is simply some normal markup so that each entry in the attachment array will have its path added as a single bullet in the final markdown file.

seed78 commented 2 years ago

Thank you so much for such a clear explanation! It works well now.