Very basic Dictionary/JSON question (v11)

I'm starting to look at using Dictionaries for some stuff, and in the v11 release notes, there's this line:

  • Added support to Set JSON Value action to delete dictionary or array entries by setting to %Delete%.

Great, just what I wanted to do: Delete an array entry from a dictionary. Let's say my dictionary is named Test, and it has a few fields: Field1 (set to Red), Field2 (set to Green), and Field3 (set to Blue). I want to delete Field2. From the release notes, I'm supposed to use the Set JSON Value action, which comes up like this:

I'm assuming my dictionary name goes where VariableName is at present. But I have no clue about the rest: Does Field1 go where .field is in the template? Or where {field} is in the template? Or both? My guess would be like this:

Set:   Test.field{Field1}
Value: %Delete%

But then I have no idea what to do about the [1] part. I've tried it with and without, but nothing seems to delete Field1.

What basic thing am I missing here?

thanks;
-rob.

Update:

I've read and tried every combination of addressing on the Set JSON Value wiki page, but still no luck.

-rob.

Not sure if this is what you want, but here it is an example showing how to delete a key from a dictionary.

Test.kmmacros (2.9 KB)

1 Like

I tried that exact structure and it fails for me. The only difference I can see is that my data is in an actual dictionary, not a JSON variable.

I’m not at my Mac, but I’ll post a simple macro either later tonight or tomorrow.

Thanks,
-rob.

Dictionaries and JSON are not at all the same.

See: Dictionaries

Use Set Dictionary Value action to set the key value to nothing and that should delete it.

image

I understand that, but did I somehow misread the blurb in the release notes? It says you can “delete dictionary or array entries…” is that a different dictionary?

-rob.

The word ‘dictionary’ turns up in different contexts.

JSON, like .plist XML, provides two forms of composite data structuring:

  • Arrays for indexed sequence
  • Dictionaries (sometimes called ‘Objects’, in a JS context) for discrete clustering of key-value pairs.

( In Python documentation they keep more narrowly to the term dictionary, and “object” is heard more rarely )

You can write JSON dictionary data to a KM Dictionary:

https://wiki.keyboardmaestro.com/action/Set_Dictionary_to_JSON

To quote the wiki entry above:

The Set Dictionary to JSON (v9.0+) action lets you set a Keyboard Maestro dictionary from a JSON dictionary.

and you also have a %JSONFromDictionary% token

but these two kinds of dictionary, while partially[1] isomorphic, are far from identical.

  • Keyboard Maestro dictionaries consist of record clusters in an sqlite3 database table, which you could query in patterns like SELECT dictionary, key, value FROM dictionaries
  • JSON, is of course, just a canonical stringification of JavaScript data structures.

  1. Partially – KM dictionary values are always atomic – strings or numbers – whereas in JSON dictionaries, the value of a key can itself be an Array or Dictionary. The depth of nesting is not constrained. ↩︎

1 Like

Thanks; that's exactly what I needed to (and clearly did not) understand. I completely misread the blurb in the release notes by assuming it applied to KM's Dictionaries. With the new knowledge that I was very wrong :), using Peter's method of just writing blank to the key works perfectly.

-rob.

1 Like