Best approach to get from abbreviation to full name

I like to set a Variable to text depending on an abbreviation for a country to get the full country name.

My list contains only the abbreviation, for example…

DE
BG
ES

With this abbreviation copied to the clipboard, I want to set the variable to Germany (from DE), to Bulgaria (from BG), etc..

Which is the best way to do this?

Thanks in advance and all the best for 2025!

With a JSON dictionary string encoding the ISO 3166-1 two letter codes that you need, paired with the full country name versions that you want, you can use the Keyboard Maestro %JSONValue% token to retrieve the match that you want.

ISO 3166 from JSON.kmmacros (2.9 KB)

2 Likes

Thank you @ComplexPoint. That looks good. But I fear I didn't make myself clear enough.

I have a sheet that only contains the abbreviation. My goal is to copy this to the clipboard and then set a variable to the full country name depending on this. I want to use this Variable in the rest of the macro.

So I need a kind of "if, then, else" condition that says: If the clipboard contains "DE" set the Variable to "Germany". Or is this already possible and I just don't see it?

I was looking for a solution other than cascading many “If, Then, Else” statements. Does this makes sense?

Thanks again.

It's already there, once you have your codes and country names in the JSON format @ComplexPoint shows above. You then retrieve the name using the %JSONValue% token.

The %JSONValue%Local_json.DE% token evaluates to

"the value of Local_json's Dictionary item whose key is DE"

...and, given the above Dictionary, would be replaced at execution with "Germany". %JSONValue%Local_json.BG% would be replaced with "Bulgaria", etc.

Thanks also @Nige_S.

Apologies but I still don't get how to implement my clipboard entry that only contain "DE" for example.

I tried to replace the "DE" with the System Clipboard token (which was the only idea I had) but it didn't work.

It can be tricky to get tokens in there -- you can "double-percent" everything else and use a "Filter" action to pre-process:

but the shortest way is to use this incantation:

%JSONValue%Local_json{%SystemClipboard%}%

...in your "Insert" action:

image

This macro will replace the two currently selected characters with the corresponding country name (you may want to include some error checking!):

ISO 3166 from JSON v2.kmmacros (2.3 KB)

Image

3 Likes

I guess there must be a huge advantage using JSON ? After all there is considerably extra characters required (4x “, 1x ,) compared to TSV format.

Thank you so much @Nige_S! That was exactly what I was looking for.

It works great and I probably have even more cases where I can use something similar.

Again, Happy New Year everyone! :fireworks:

Fewer characters brings an advantage that I am missing ?

With a dictionary and a JSON engine, the value for a given key is found for you.

( With TSV, you are thinking of a line-by-line search ?)

Yes. Line by line. JSON is faster? In general, less/simpler is better, isn’t it? (I’m not a programmer )

JSON is faster and simpler because you don't have to go line by line.

You just supply the key, and you are given the corresponding value.

2 Likes

Thank you Rob. All the best for 2025!

1 Like

Gelukkig nieuwjaar !

( hash table vs linear search is the underlying pattern there – in the contrast between keyed dictionaries and line-scanning. The advantage, if any, is trivial with short lists, but grows quite rapidly with scale )

2 Likes