How to Generate a JSON tiddler from a text tiddler!

Folks,

This was corrected and now works, use the macro and provide the input.txt, but only with the help of @EricShulman and @Mark_S thanks gentlemen.

\define temp() $:/temp/text-to-json-tiddler
\define text-to-json(tiddler)
<$button >Convert $tiddler$ to $tiddler$.json
<$action-createtiddler $basetitle="$tiddler$.json" $savetitle=<<temp>> type="application/json"/>
<$list filter="
[[$tiddler$]get[text]splitregexp[\n]!is[blank]]" variable="row-content">
   <$action-setfield 
         $tiddler={{$:/temp/text-to-json-tiddler}} 
         $index=<<row-content>>
         $value=""
    />
</$list>
   <$action-deletetiddler $tiddler=<<temp>>/>
</$button>
\end
<<text-to-json "input.txt">>

Original Post
Once resolved this will be a how to instruction. I will edit the lead post. However I cant get it to work at present. Any help appreciated.

\define text-to-json(tiddler)
<$fieldmangler>
<$button>
Convert $tiddler$ to $tiddler$.json
<$action-createtiddler $basetitle="$tiddler$.json" $savetitle="$:/temp/text-to-json-tiddler" type="application/json">
<$list filter="[[$tiddler$]get[text]splitregexp[\n]]" variable=row-content>
   <$action-setfield 
         $tiddler={{$:/temp/text-to-json-tiddler}} 
         $index=<<row-content>>
         $value="dummy"
    />
</$list>
</$button>
</$fieldmangler>
Show preview? change to filter?

<$list filter="[[$:/temp/text-to-json-tiddler]has[title]]">{{||$:/core/ui/Buttons/edit}} <$link/> {{||$:/core/ui/Buttons/delete}}</$list>
\end
<<text-to-json "input.txt">>

In the above example the input.txt has a number of lines containing single words. I want to use these to create a JSON file so later I can check if a given word matches one in the data tiddler.

Currently the result is only an empty tiddler with type=application/json

  • Could it be the fieldmangler widget, is it needed is it in the correct place.
  • have I a syntax error
  • is the create tiddler too soon for the add indexes to work?

Thanks in advance!

1 Like

Here’s my version of your code. The main problem was “the create tiddler too soon for the add indexes to work?”

The solution is to split the button actions into two parts:

  • create the tiddler in the body of the $button
  • set the indexes in the $button actions="""...""" param

This allows the $action-createtiddler to finish before the $action-setfield’s are invoked

See notes below.

\define temp() $:/temp/text-to-json-tiddler

\define text-to-json(tiddler)
<$button actions="""
   <$list filter="[[$tiddler$]get[text]splitregexp[\n]]" variable=row-content>
      <$action-setfield
         $tiddler={{{ [<temp>get[text]] }}} $index=<<row-content>> $value="" />
   </$list>
   <$action-deletetiddler $tiddler=<<temp>>/>
""">
Convert $tiddler$ to $tiddler$.json
<$action-createtiddler
   $basetitle="$tiddler$.json" $savetitle=<<temp>> type="application/json"/>
</$button>
\end

<<text-to-json "input.txt">>
  • moved temp tiddler name into a macro for code readability (and easier to change if desired)
  • no <$fieldmangler> needed
  • $button actions="""...""" is invoked after “inline” $button actions are completed
  • no “dummy” value needed, just use ""
  • after creating new tiddler and setting indexes, delete temp tiddler to cleanup
  • closing / was missing from $action-createtiddler

enjoy,
-e

1 Like

I see Eric beat me. Oh well. For what it’s worth, here’s my version

\define text-to-json(tiddler)

<$button >Convert $tiddler$ to $tiddler$.json
<$action-createtiddler $basetitle="$tiddler$.json" $savetitle="$:/temp/text-to-json-tiddler" type="application/json"/>
<$list filter="[[$tiddler$]get[text]splitregexp[\n]!is[blank]]" variable="row-content">
   <$action-setfield 
         $tiddler={{$:/temp/text-to-json-tiddler}} 
         $index=<<row-content>>
         $value="dummy"
    />
</$list>
</$button>

Show preview? change to filter?

<$list filter="[[$:/temp/text-to-json-tiddler]has[title]]">{{||$:/core/ui/Buttons/edit}} <$link/> {{||$:/core/ui/Buttons/delete}}</$list>
\end
<<text-to-json "input.txt">>
1 Like

Thanks @EricShulman and @Mark_S

I now have it working, for some reason Eric’s was not working for me however I like the design methods and built a hybrid;

\define temp() $:/temp/text-to-json-tiddler
\define text-to-json(tiddler)
<$button >Convert $tiddler$ to $tiddler$.json
<$action-createtiddler $basetitle="$tiddler$.json" $savetitle=<<temp>> type="application/json"/>
<$list filter="[[$tiddler$]get[text]splitregexp[\n]!is[blank]]" variable="row-content">
   <$action-setfield 
         $tiddler={{$:/temp/text-to-json-tiddler}} 
         $index=<<row-content>>
         $value=""
    />
</$list>
   <$action-deletetiddler $tiddler=<<temp>>/>
</$button>
\end
<<text-to-json "verbs.txt">>
  • Mark what was your change (just close ? />)
  • Eric I could not see why yours did not work
  • Not withstanding that may I ask will the imbedded actions always go first and be completed before the actions parameter in the button?
    • I have an existing “issue” (stemming from the use the cookies Eric helped me with) where I want to save the tiddler, set some cookies then reload. It appeared to never finish saving before reloading. I will try the reload inside the actions parameter.

Thanks so much Gentlemen

[Edited] add \end after the button so this works out of the box

1 Like