I can write simple index/value pairs into a JSON tid easily, but properly adding to nested JSON has become the bane of my sanity!
Given an example JSON structure like this in a tiddler named “SomeTid”:
{
"sale1": {
"discount": "50",
"price": "10.50"
},
"sale2": {
"discount": "60",
"price": "8.00"
}
}
I managed to figure out how to add a new entry of “sale3: discount=60, price=5.99” with this action widget:
<$action-setfield $tiddler="SomeTid" $field="text" $value={{{ [[SomeTid]get[text]jsonextract[]jsonset:object[sale3]jsonset[sale3],[discount],[60]jsonset[sale3],[price],[5.99]] }}} />
…but this ugly ‘sledgehammer’ method forcibly overwrites the whole text field of the JSON tid – which in clumsy hands (like mine!) has very high potential to cause the complete loss of its data. (It also squishes the human-readable “pretty” formatting into a single, compact line that’s terrible to read or hand-correct.)
With simple, non-nested JSON, I can use the $index mode of $action-setfield
or $action-listops
and it safely adds the new index and value, leaving the existing ones alone – and it also doesn’t “break” the tid’s appearance.
The “Modifying JSON tiddlers” documentation only says to use either of those widgets to modify JSON, but I can’t figure out how to make them add nested JSON without a full-overwrite? I’m not sure what the $value (or $filter and/or $subfilter) need to be to make it work. Everything I’ve tried so far has resulted in the JSON being escaped in either the index or value when it does anything at all. For example, this:
<$action-listops $tiddler="SomeTid" $index="sale3" $filter="[[]jsonset[discount],[60]]" />
…correctly inserts a new “sale3” index like I want, but the value it writes is "{\"discount\":\"60\"}"
with the JSON-escaping, which is no good.
Can anyone pretty please lend me a hand to get this working, or tell me if it’s just not possible? I know I’m probably missing something painfully obvious, but I’ve searched the TW docs up and down for weeks now trying to figure out what it, and I think I’ve officially fried my poor noggin at this point.