How to convert Unicode to characters?

I want to convert come unicode (format: \uxxxx) to characters, how can I do it in Keyboard Maestro?

This might be one approach:

(note that you need quotes around the string – either single or double will do, as long as they are consistent)

->

1 Like

Thanks, @ComplexPoint.

Wow. Didn't know I can simply do this.

However, I should give more details about my need.
My string is JSON encoded like this:

{"ok":true,"result":{"mid":2161,"from":{"id":12345,"username":"uname"},"chat":{"id":12345,"type":"private"},"date":1598795225,"text":"\u6d4b\u8bd5"}}

I need to convert the unicode part into characters. The rest should remain the same.

I found the solution from this post:

Using JSON.parse(myVar); will do the job for me.

Thank you for pointing me to JXA. I could search a solution for it.

1 Like

Keyboard Maestro tokens support %xxxx% for Unicode, eg:

image

1 Like

Thanks, @peternlewis. That's helpful to know.

So far, I know deal with a real case like converting a Unicode embedded string as:

{"ok":true,"result":{"mid":2161,"from":{"id":12345,"username":"uname"},"chat":{"id":12345,"type":"private"},"date":1598795225,"text":"\u6d4b\u8bd5"}}

I'm wondering (for learning) how this display text in a window can be used in a real scenario? In my case, first, we will have to get all the codes out (we don't know how many they will be). Then we need to add % to before and after each code.

What is your actual purpose? Things are encoded for a reason, and just decoding them will cause problems unless your only objective is to visually look at the string. Otherwise it could be an encoded double quote within the double quoted string, or some other character that has some meaning.

Similarly, if you just replace the \u6d4b with %6d4b%, and then process the tokens, you could have other problems because of other percent or backslash characters in the text.

If you aren't worried about other potential problems with your text, then you can convert the \u characters to tokens and then process the tokens and then display the text.

image

Yes. I just need to see the text. No \ or % in other places as far as I know.
This works for me.
Thanks!

1 Like

I have encountered a very strange thing.

When I set the variable to text, it automatically strip off \u in the text. So the following actions won't work.
image

image
If I escape \, it works.

Another problem is with \ud83d\udca1 (Need to use \\ud83d\\udca1) to set variable.
I believe it is a light bulb emoji (consisting of two Unicode)?
It won't be converted to the bulb emoji.

Adjust the token processing in the gear menu of the Set Variable action - configure it to Process Nothing.

Otherwise \u has meaning to Keyboard Maestro (specifically is uppercases the following letter).

image