How to develop a new filter operator for json data

I know how to develop a filter operator for a list of tiddlers in my leaflet plugin which takes tiddlers to generate markers on the leaflet map.

In next step, I want to store data in a json tiddler (e.g. Australian Capital Cities in json data.

However, I am not sure how I should implement it here.

For example, This filter [[json/AustraliaCities]addmarkers[]] will pass the json tiddler title into filter, and then I have to parse json object in the js codes.

Are there any ways to get the items using filter operator in the json object?

Not sure my question is not clear. Thanks for any suggestions.

1 Like

If you search tiddlywiki.com there are a number of ways to manipulate json and also making use of the index parameter in some widgets as well.

What do you think is missing?

See also json mangler plugin.

There is also a summary of json here in talk tiddlywiki

See this How do TWs JSON Formats Look Like - #2 by pmario

In your JS code, once you get the tiddler’s title, you can use something like

var obj=JSON.parse($tw.getTiddlerText(title));

to fetch the tiddler’s JSON text and convert it into an actual javascript object.

Then, you can add your marker objects to that javascript object.

When you are done, you can convert the javascript object back into JSON text and save it to the tiddler’s text field using something like:

$tw.setText(title,"text",null,JSON.stringify(obj),{});

-e

Thanks @EricShulman, my original thought is as your method. But it sounds this method is not too generic as it depends on data structure in json.

I might use this method as a starting point.

Won’t it always? For instance your add-markers would likely not work with this JSON input:

{
  "Sparkles": {"species": "unicorn", "age": "timeless", "color": "rainbow", "desc": "magical"},
  "Butch": {"species": "unicorn", "age": 4, "color": "gray", "desc": "chubby", "subspecies": "rhinoceros"}
}

So if you’re going to be doing this in JS, then it will be tremendously easier to do this as Eric suggests.

If you want to do it in wikitext, creating a function whose name includes a “.” (perhaps add.marker?), then Tony is right, there are JSON tools for manipulating JSON strings. They won’t be as flexible as doing it in JS, but they will be more familiar to other TW users.

But I don’t think there’s any technique that will let you work on an arbitrary JSON string.

1 Like

Thanks @Scott_Sauyet for your suggestions, I will work with geojson which might be much simpler.

Have you looked at @jeremyruston’s comprehensive geospatial plugin?. It is also a wrapper for leaflet.

It implements two widgets which work together (geomap/geolayer) and 9 filter operators.