farling42 / obsidian-import-json

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

Use cases? #17

Closed metawops closed 2 years ago

metawops commented 2 years ago

Sorry for opening an issue for my (simple) question/suggestion but the "discussion" module is not active for this repository. πŸ€·β€β™‚οΈ

My suggestion is that you spend a sentence for the use case of your plugin in the README.md file. That's because I thought I could use this plugin for my use case but I think I can't.

My use case would be: I have a single JSON file which basically contains one array. The array contains dictionaries and inside these are key/value pairs I'd like to read and transform into one single markdown table in one single note (i.e. markdown file).

Alas, in the README.md file I read about notes (plural) that get created from your plugin?! So I was wondering what the initial use case for your plugin was and if my use case could make use of your plugin at all? 😳

farling42 commented 2 years ago

Hi, I can certainly update README.md to make it clearer.

Converting a CSV file into a single MD table is a trivial action which can be done via any CSV-to-markdown web site.

This module provides the ability to take a CSV file and create a separate note for each row in the file. This is useful for many situations, e.g. where you have a product list in an excel spreadsheet and you want to create a separate obsidian note for each product.

metawops commented 2 years ago

Yes, I know about CSV to Markdown – but not about JSON to Markdown (thatβ€˜s why I asked specifically about JSON in my question, not CSV). In any case this manual step in between (via a web page) is needed which is annoying. Being able to directly import JSON data into a Markdown note in Obsidian would be nice. (Maybe thatβ€˜s an opportunity for my 2nd Obsidian plugin … πŸ€”πŸ˜‰)

Thanks for giving that example use case! Makes sense (and I can now uninstall this plugin).

farling42 commented 2 years ago

Hi, I've just checked my module code and it seems that it can treat part of a JSON file as a single object.

It uses the "Field containing the data" to identify the top-level field within the JSON object from which to fetch the data (nested fields are separated using "." notation; e.g. "top.second.third").

If the "Field containing the data" is supplied, then it will use that field as the top of the JSON data. If that referenced field is not an array then it will be converted into a single element array. The resulting array (present in JSON, or created locally) will then be used to create one or more Notes from the markdown template.

If the "Field containing the data" is empty, then if the JSON file is an array then that array will be used to create one or more Notes from the markdown template. If the JSON file is NOT an array, then the top-level fields in the top-level object of the JSON file will be searched to find the first element which is an array, and then that array will be used to create one or more Notes from the markdown template.

So if you supply a value for "Field containing the data" then it can process that part of a JSON file as a single object, allowing your markdown template file to use a {{#each name}} loop to cycle through the entries in an inner array to create your markdown table.

metawops commented 2 years ago

Thanks so much, Martin, for extra-checking! Because I'm totally new to handlebars templates I'll have to learn those concepts first before I can try something with my JSON file. My JSON file is at top level a dictionary containing one single key/value pair. The key is "stats" and its value is an array. So I think I should put "stats" (without the quotes) into the "Field containing the data" input box, right? I'm wondering, however, what to put into the "Field to use as Note name" input box because I like to get just one single note whose name doesn't have to depend on a data field in the JSON file ... πŸ€” And the handlebars template file should then template my table whose rows correspond to data from each array element. I wonder how this template file will look. But I'll figure it out! πŸ˜‰

metawops commented 2 years ago

Okay, here's a screenshot of my JSON file:

Screenshot 2022-06-29 at 11 12 31

And here's the markdown handlebars template file I came up with:

Screenshot 2022-06-29 at 11 11 23

And here's the settings I use with the plugin:

Screenshot 2022-06-29 at 11 11 06

Although I get an "Import finished" from the plugin, no file gets created inside my vault. πŸ€·β€β™‚οΈ

Any hints/tips/ideas for me here? 😳

metawops commented 2 years ago

HA! I found a way to import my JSON data directly into a table and visualise it, too. The famous Dataview plugin has a function to read any text file into a string. I can then easily parse that into a JSON object and create a table – all inside a dataviewjs code block. Plus, with the "Obsidian Charts" plugin I can visualise that data in a chart – in the same dataviewjs block, too. 😊 So, currently no need anymore for using your plugin, Martin, but it sounds great and I'll let it installed! πŸ˜‰

farling42 commented 2 years ago

That's good to know. :-)