farling42 / obsidian-import-json

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

JSON-Elements with no children (hence, no values) causes failure of import #56

Closed buttonpushertv closed 8 months ago

buttonpushertv commented 8 months ago

Working with a JSON that has elements with no children causes the import process to fail.

(I'm fully willing to accept that I may not be addressing this correctly, from a programming standpoint; I will confess to knowing enough to be dangerous with javascript & handlebarjs).

Looking at a Fantasy Map Generator JSON, the pack.provinces[0] element or the pack.burgs[0] element both are simply represented as 0,, even though there are many more child elements after that first empty element(burgs[1],[2],[3], etc). Importing either of these branches causes an error: Cannot create property 'SourceIndex' on number '0'

Is this a bug of the importer? Or is this bad data construction within the JSON file (as in, should those items at least have some dummy child elements)?

Manually adding in a dummy entry at the 0 position allows the import to succeed, but is there a way to tell the importer to ignore entries like this?

I do have some error checking code within a custom helper that will fail if an element contains 0 or is undefined, but the importer fails and stops the import before any custom helper code is even run.

farling42 commented 8 months ago

I can see the issue, in that the first element of the pack.provinces array contains the numeric value 0 instead of an object.

Whilst that is technically a valid JSON array, it doesn't seem sensible to have an array where one element is a number and the remainder of the elements are objects.

It looks like this also applies to the pack.features array.

Whilst the first object of th epack.burgs array is an empty object.

Whilst I could add some extra logic to simply ignore any elements which are not a JSON object, the better solution would be for the web site which is generating the data to produce a sensible array of data in the first place.

buttonpushertv commented 8 months ago

I thought along the same lines. I will reach out to Azgaar and see if it can be updated.

For the time being, it's easy enough to manually add in a placeholder element to allow the importer to do its job.

Thanks.