Hey there,
I’m still stuggling with JSON. I do understand now how to read and write data in JSON. Now I’m trying to delete for example “b” in the example below:
{
"a": "one",
"b": "two",
"c": "three",
}
The only JSON-Operator that is changing the data is jsonset, as far as I know. But I don’t find a solution with it.
When working with flat JSON structures like your example, it’s much easier to use the $action-setfield widget with the $index attribute and no$value. It’s somewhat unintuitive, but the key phrase is here in the docs (bolded for emphasis):
$value
The value to be assigned to the field or index identified by the $field or $index attribute. If neither is specified then the value is assigned to the text field. If no value is specified, $field or $index will be deleted.
So to delete index “b”, you could use
<$action-setfield $tiddler="MyJSON" $index="b" />
And conversely, to add a new index “d”, you could use
In either case (as with any $action- widget), $action-setfield needs to be used with a trigger like $button. So your functional code might look something like this:
<$button>
delete index "b"
<$action-setfield $tiddler="MyJSON" $index="b" />
</$button>
IMO, since $action-setfield sets the precedent of using a single action widget to set both fields and indexes, you might reasonably expect $action-deletefield to have a similar affordance. But $action-deletefield doesn’t support an $index attribute (at least as of v5.3.8), so we have to use this $action-setfield workaround for indexes.
Unfortunately, TW isn’t really (1) optimized (2) for working with nested JSON structures. Personally, I’d love to see a core modification to $action-setfield like the one @EricShulman suggested here (or even a new $action-setjson widget, as @jeremyruston suggested last time I brought this up). This would presumably let us use the missing-$value trick to delete an index, in addition to making it generally easier to set JSON index values. But in the meantime, I have to recommend you try to avoid complex JSON objects if at all possible.
Yes, that was me. Even if I decided to use several tiddlers and only data dictionary tiddlers, because thats way easier to handle in my case. Plus it is too easy for me to mess up the Jsontiddler with jsonset.
However, if you want to delete a nested JSON object (i.e., NOT a top-level index), then the above $action-setfield widget cannot be used… at least, not directly.
Instead, you need to use a combination of jsonextract and jsonset filter operators to fetch the existing JSON tiddler content and construct a new nested JSON definition that excludes the desired object. Only then can you overwrite the JSON tiddler’s entire text field to replace the existing JSON definition with the newly constructed JSON definition.
I will just add that hierarchical JSON’s can be flattened, easily with a click using the JSON Manger Plugin. GitHub my wiki copy here
The result is one compound key that can be retrieved and read getting its value and presumably deleting by giving it a “” value. Theoretically you can search the resulting compound keys to find the one (or more) you want.
I have not tested this yet
Apparently you can set a value to nul and it will return nothing and not delete the item