[TW5] access data in nested json

270 views
Skip to first unread message

Hans Maulwurf

unread,
Jun 28, 2016, 9:51:37 AM6/28/16
to TiddlyWiki
is it possible to save the following as json-tiddler and access e.g. the Strings BMW, Toyota etc.? at tiddlywiki.com I only found how to get the "object" like {{jsontiddler##type}}

{
  "type": "object",
  "title": "Car",
  "properties": {
    "make": {
      "type": "string",
      "enum": [
        "Toyota",
        "BMW",
        "Honda",
        "Ford",
        "Chevy",
        "VW"
      ]
    },
    "model": {
      "type": "string"
    },
    "year": {
      "type": "integer",
      "enum": [
        1995,1996,1997,1998,1999,
        2000,2001,2002,2003,2004,
        2005,2006,2007,2008,2009,
        2010,2011,2012,2013,2014
      ],
      "default": 2008
    }
  }
}


Mark S.

unread,
Jun 28, 2016, 10:45:08 AM6/28/16
to TiddlyWiki
According to TiddlyWiki.com, "Note: It is currently only possible to retrieve data from the immediate properties of the root object of a JSONTiddler."

It appears that JSON.parse is available (at least in FireFox), so it might be possible to create a javascript macro that could pull out values from a more complex json data structure.
 
Mark

Hans Maulwurf

unread,
Jun 28, 2016, 1:24:58 PM6/28/16
to tiddl...@googlegroups.com

I'm New into this. Could someone give me a hint/howto/tutorial for doing this?

Mark S.

unread,
Jun 28, 2016, 3:43:39 PM6/28/16
to TiddlyWiki
Hello,

I threw this together. It's probably not very robust -- I would need to know more about your use case. But it should give a starting point for thinking about the problem.

BE SURE TO BACK UP YOUR TW BEFORE TRYING!!

To use, drag, drop, and import the attached JSON tiddlers into your TW. Save and reload.

The "Test TestJSONData" contains this line:

<$macrocall $name="parsejson" data={{TestJSONData}} idx1="properties" idx2="make" idx3="enum" idx4="2" />


(Returns "Honda")

Each index level into the project is called with "idx1", "idx2", etc. If you have idx3, you must also provide idx2 and idx1.

Up to 4 index levels are possible. More levels could be added.

Eric Shulman provides a nice introduction to javascript macros here:

https://groups.google.com/forum/#!topic/tiddlywiki/M6wADVDjyTE

(last post, I think).

HTH
Mark

Project_JSON1.json

Hans Maulwurf

unread,
Jun 28, 2016, 5:14:42 PM6/28/16
to TiddlyWiki
Mark, thanks a lot for your work, I will have a look how i will use it.

For my use case, I want to read out an array of a json-idx which contains tiddler-names and to use them (e.g. just linking to these tiddlers as a list or insert their content). So in the worst case, I don't know the size of this array. (that's why it's difficult to use ur solution with giving the idx)

Did nobody before wanted to access such a json object?

Mark S.

unread,
Jun 28, 2016, 6:20:06 PM6/28/16
to TiddlyWiki
This is why the use-case is more important than the example. It doesn't sound like your use-case requires the index depth I had supposed from your example.  Probably need to see more of the actual data you are interested and understand why you want it in JSON format.

Good luck,
Mark

Hans Maulwurf

unread,
Jun 29, 2016, 2:25:07 AM6/29/16
to TiddlyWiki
I thought it would easily be possible because $:/HistoryList is also a nested json?
Reply all
Reply to author
Forward
0 new messages