Accessing JSON data tiddlers

Hi there, haven’t been here for quite a while. Regular TW user but not coding frequently enough to gain routine.

Anyway, I wanted to use version 5.2.5. to upgrade some Wikitext code I did about two years ago. I used Josh Fontenay’s JSONMangler plugin then, but wanted to upgrade the code to the new json.data access function (BTW thanks so much for including these).

I have a set of JSON data tiddlers which are doubly nested, and I want to iterate through them to display their content in a tabular way.

I started out using the instructions in this thread: How to iterate on a dictionary (and an array) from a json data tiddler? - Discussion - Talk TW (tiddlywiki.org), and was able to access individual tiddlers, but failed when trying to loop through them, like this:

<$list filter="[tag[myTag]type[application/json]]" variable="myVar">

    <!-- this works -->
    {{{ [<myVar>get[text]] }}}

    <!-- this doesn't work -->
    {{{ [<myVar>jsonget[myJSONkey] }}}

</$list>

Any clue what I am getting wrong here. How do json… function behave in a different way when accessing a tiddler?

On a side note: would it be possible to include jsonget[] in a list filter in order to filter for a given json key? Tried it, but, as expected, no avail.

Thanks so much for helping me out on this. Still can’t seem to wrap my head around certain concepts and intricacies.

1 Like

Hi @Werner the problem there is that myvar contains the title of the data tiddler. The json operators require that the JSON data itself be the input data, not a tiddler title. This is done sot that we can manipulate JSON within variables as well as tiddlers.

So one way to fix the code would be like this:

<$list filter="[tag[myTag]type[application/json]]" variable="dataTiddlerTitle">

    {{{ [<myVar>get[text]] }}}

    {{{ [<myVar>get[text]jsonget[myJSONkey] }}}

</$list>

Thanks Jeremy, I actually came across this, but I just wanted to have it confirmed, because it just did not seem right. There is one issue I still have: trying to access an array (say for counting its items) with jsonindexes[] returns a string like “0123”. Is this the correct behaviour?

Thanks, Werner

The jsonindexes operator returns the indexes of the array as separate list items. I think they are appearing to be running together because the {{{x}}} syntax doesn’t apply a separator to the results.

You should be able to count the entries in an array with something like `[jsonindexes[mykey]count[]].

Thanks Jeremy, awesome. Behaves as expected.

Furthermore, I was able to solve my “side note” problem using jsonget in a filter statement:

This

<$list filter="[tag[myTag]type[application/json]get[text]] :filter[jsonget[myField]match[mySearchItem]]" variable="myVar">
...
</$list>

did the trick perfectly.

1 Like

Great, thanks @Werner – these operators are still quite new and so there may yet be unexpected pitfalls or shortcomings, so do let us know how you get on.

Actually, the way the operators behave on arrays are somewhat counter-intuitive.

Example: jsonget[] on an array of objects returns the fields per array item in alphabetic order. So, jsonget[Array]count[] doesn’t work, you have to work around this via jsonindexes[]. It’s all manageable, but it took me some time to figure out.

Anyway, I’m more than happy that you have included these JSON functions, so I can access my JSON data tiddlers again, after Jsonmangler ceased to work.

Plus, I’ve discovered the beauty of the reduce operator. But that’s a different story.

It wasn’t obvious what jsonget should do in that situation, given that we already had to introduce the jsonindexes operator. Returning the values seemed the most consistent. The reason for the sorting is to avoid accidentally exposing the implementation-specific browser sorting of object keys.

1 Like

Thanks, yeah, makes sense. As I said, once I figured it out, it worked as supposed to

FYI: I have being successfully using JSON Mangler v 2.2.5 On tiddlywiki V5.2.2 to import CSV to a plugin tiddler.