joshuafontany / TW5-JsonMangler

A JSON extension plugin for TiddlyWiki5
https://joshuafontany.github.io/TW5-JsonMangler/
MIT License
15 stars 2 forks source link

Search on Json #5

Closed oxydum closed 5 years ago

oxydum commented 5 years ago

Hi Joshua,

I'm looking for a way to retrieve data from keys like in this myjson:

[{"key":10,val:"A"},{"key":11,val:"B"},{"key":12,val:"C"}]

<<search key="11">> to retrieve "B" I know to get this by rank : {{myjson##/1/val}}

Thanks

joshuafontany commented 5 years ago

Hmmm, great question. It took me a couple of read-throughs to fully understand your query.

One thing to note. JSON sytax requires all key-names to be strings (wrapped in quotes). Your val keys should be "val".

tOk, so The first way I would approach it is a macro with some $list widget action.

\define searchKeyArray(keyName)
<$list filter="[<currentTiddler>indexes[]suffix[key]]" variable="index">
<$list filter="[<currentTiddler>getindex<index>regexp[$keyName$]]" variable="selectedItem">

keyPath = <<index>>

<$list filter="[<index>removesuffix[key]addsuffix[val]]" variable="valPath">

valPath = <<valPath>>

val = {{{ [<currentTiddler>getindex<valPath>] }}}

/* More wikitext here that will let you set the pulled value to another temporary tiddler, or otherwise manipulate this specific index. */

</$list>
</$list>
</$list>
\end

<$tiddler tiddler="TestJson">
<<searchKeyArray keyName:"11">>
</$tiddler>

I have tested this in my demo wiki and it works out pretty well. Let me know if that is helpful, and thanks for the question!

joshuafontany commented 5 years ago

Cool. I'll close this issue then. Mahalo (thanks) for using my plugin!