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.
<$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.
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.
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?
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].
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.