Importing a JSON file as text

I have a JSON file which is a valid dump of tiddlers. When I want to import it as text into a single HTML file wiki, I have to:

  • create a new tiddler
  • set its content type to text
  • open the JSON file in a text editor, select everything, copy, then paste it into the text field of the new tiddler, then save the tiddler

If I try to import it via TiddlyWiki’s Import button, it makes the assumption for me and only suggests importing the JSON content as separate tiddlers. There’s nothing like an “Import as plain text” checkbox in the Import tiddler dialog.

Is there an easier way than the described above to import a JSON file as a piece of text?

  • Open the JSON file in a text editor
  • At the top of the file insert
title: youTitleComesHere
type: text/plain

[{your json data comes here}]
  • The new-line between type and the first json character is important
  • Rename the file to abc.json.tid
  • Import into TW.

That should be it

This still involves manual intervention, but thanks for the idea! I can just write a shell script that will perform this file transformation :+1:

Here’s a bit of wikitext that adds a “Save as JSON” interface to the bottom of the $:/Import tiddler:

First, create a tiddler (e.g “SaveImportAsJSON”), tagged with $:/tags/ViewTemplate, containing:

<%if [<currentTiddler>match[$:/Import]] %>
Save as JSON: <$edit-text field="newtitle" default="New JSON Tiddler"/>
<$button>save
<$let newtitle={{{ [{!!newtitle}!match[]else[New JSON Tiddler]] }}}>
<$action-deletefield plugin-type status newtitle/>
<$action-setfield title=<<newtitle>>/>
<$action-deletetiddler $tiddler="$:/Import"/>
<$action-sendmessage $message="tm-close-tiddler" $param="$:/Import"/>
<$action-navigate $to=<<newtitle>>/>
</$let>
</$button>
<%endif%>

Then:

  • Drag-and-drop your JSON file into your TiddlyWiki (or use the “import” button from the sidebar “Tools” tab)
  • The $:/Import tiddler will be displayed with the added “SaveImportAsJSON” controls at the bottom
  • Enter a tiddler name (or use the default “New JSON Tiddler”)
  • Press the “save” button, which performs the following actions:
    • Remove the plugin-type, status and newtitle fields from the $:/Import tiddler
    • Copy the $:/Import tiddler to the title you have entered
    • Delete and close the $:/Import tiddler
    • Open the newly created JSON tiddler in the StoryRiver

enjoy,
-e

I like this method because it seems like an easy way to intercept the import process and perform my own custom import. I could be wrong but it seems to miss a step.

The new JSON tiddler created is using $:/Import format like this

{
   "tiddlers": {
        "..<title>…” : { "title": "..<title>...", "text": "..<text>..." ... } 
         ...
   }
}

which is different from the original tiddlers JSON format [ { "title": "..<title>...", "text": "..<text>...", …} ... ]. So IF the original tiddlers JSON format is required, another step is needed to extract from “tiddlers” ?

I tried an alternate approach using dropzone, which is how I do custom import :

<style>
.jsontiddler-dropzone{
	margin:4px auto;
	border:2px dotted green;
	text-align:center;
}
.jsontiddler-dropzone:focus {
	background: #fffedd;
}
</style>

<$dropzone
			importTitle="json_imported_as_text"
            deserializer="text/plain"
            files only="yes"
		>
			<div class="jsontiddler-dropzone" tabindex=1>Drop your JSON file here</div>
</$dropzone>

This seems to work as the content of the JSON tiddlers are indeed imported into the text field of the new tiddler, but the type is “application/json” ?

There is another way but i dont have access to my desktop to get my notes.

From memory set up a custom $browse widget and browse for .json files but force the plan text deseriliser or similar. It will not attempt to import it as json tiddlers.

Alternativly in import rename the $:/import tiddler and edit the fields to remove the plugin type. Although this can be set to plugin too.

What remains in the content of the import, the json

Alternativly in import rename the $:/import tiddler and edit the fields to remove the plugin type. Although this can be set to plugin too.

What remains in the content of the import, the json

That’s what Eric’s solution does, which he deftly packed into a SAVE button. I suppose old-timers here like yourself remembered it as a nifty trick to save imported content in json format :heart:

From what I tried, it’s in a specific json format used internally by $:/Import, which TW may have some useful routines for this format. In this case, It’s not exactly the same as the original json content, but may not matter for OP’s usage.

From memory set up a custom $browse widget and browse for .json files but force the plan text deseriliser or similar. It will not attempt to import it as json tiddlers.

Have not tested this. The mechanism seemed similar to the drop zone I tried, and may face similar issue. What I think happened is the drop zone correctly deduces that the content is “application/json” and set the tiddler type accordingly, but switch to the “text/plain” deserialiser as instructed, which processes the content correctly, just doesn’t change the tiddler type. The tiddler type can be changed post import if needed, so not a big problem.

Yes the browse widget works like the import widget but you can choose the deseralser/type.

Change the file name to a .txt

Ah, yes ! If .txt is used instead of .json for the tiddlers json file, the drop zone does the importing and creates a tiddler of type “text/plain”.