Using jsonstringify to stringify tiddler text

Hello,

I’ve been trying to use a json data tiddler to store tasks. To this effect, I have an edit-text widget editing a temporary state tiddler, and I want to store the text of that to the data tiddler once I’m done.

I can’t get it to work, however. I’m using the action-setfield widget, and $value should be the text of the tiddler. but no filter works: [[$:/state/Task]+jsonstringify[]] doesn’t work, [{$:/state/task}+jsonstringify[]] doesn’t work, a bunch of other things I tried don’t work.

Is my model of how filters work incorrect, or is the jsonstringify operator itself a bit finicky?

You do not need the JSON operators if setting values in a data tiddler, instead use the actionsetfield $index operator to hold the key to the record, and the tiddler to name the data tiddler.

I think you need to share a little more for us to help you, are you using filtered transclusions such as $value={{{ filter }}}.

In your own debug process consider creating little fragments of the overall solution to see if they are doing what you expect or returning the values you expect.

Have a closer look at the actionsetfield docs about the $index parameter as Tony mentioned.

And to read the stuff you can use the getindex operator

If that’s not enough you can have a closer look at my KeyValues — advanced data-tiddler functions plugin.

1 Like

I’ll expand a bit.

I’m trying to create a list of tasks. The structure is the following: the tasks are stored in a data tiddler, with the keys being the current timestamp and the values being the description of the task. The workflow is that you type your task in, press enter, and it gets stored in the tiddler. Some example code:

<$keyboard key=enter actions="<$action-setfield $tiddler=QuestList $index=<<now [UTC]YYYY0MM0DD0hh0mm0ssXXX>> $value=[{$:/state/TempQuest}+jsonstringify[]]/>">

<$edit-text tiddler="$:/state/TempQuest" tag=input placeholder=“add new…”/>

</$keyboard>

I could just transclude the text of the tiddler in the $value argument, but there might be instances where there are characters that JSON can’t inherently handle, so I’d like to escape them.

I would expect my code to just work. If I have [[text]+jsonstringify[]], that works fine. But transcluding the text in, and then trying to run it through jsonstringify, doesn’t work.

the fundamental idea is just me wanting to jsonstringify the text of a tiddler, and store that text as a value of a key-value pair in a JSON data tiddler.

There’s no need to use jsonstringify[] here! By default, when $action-setfield writes a value to an index, the tiddler is created with type=application/json, and the index value is automatically “stringified” to be json compliant.

Thus, you can just use: $value={{$:/state/TempQuest}} in the $action-setfield widget.

Also, while jsonstringify[] is not needed for this use case, the syntax of the filter you were using was incorrect.

To take the temp tiddler content and apply the jsonstringify[] filter operator to it, you would surround the filter syntax with tripled-curly braces to indicate that “filtered transclusion” is to be used to compute the $value parameter, like this:

$value={{{ [{$:/state/TempQuest}jsonstringify[]] }}}

Note also that the ‘+’ is a “filter run prefix” (the abbreviated equivalent to :and[...]) and is only needed when applying a separate filter run to the results of the filter run(s) that precede it in the same filter, like this:

$value={{{ [{$:/state/TempQuest}] +[jsonstringify[]] }}}

enjoy,
-e

2 Likes

Thank you for the help! $action-setfield automatically stringifying json helps.

Thanks also for the correction on filter syntax-I was doing it that way since I saw + being used in the examples. I’m aware that, in general, you would just use an operator directly without the +, I just thought jsonstringify was different in that respect.