%JSONFromVariables%prefix% - prefix is case-sensitive?

@peternlewis I just discovered that the prefix in %JSONFromVariables%prefix% is case-sensitive.

Why? Variable names aren't case-sensitive. This is especially troublesome when I want to write code to get local variables. People use prefixes like "local" or "Local" or "LOCAL".

I'll have to write some JXA to deal with this for now.

Any thoughts of changing this?

JSON keys are case-sensitive

Any thoughts of changing this?

Certainly hope not :slight_smile:


ECMA-404 - Ecma International

3 Likes

Hmmm. I guess you might have a point. It's only my specific use case that wants it to act differently.

OK, @peternlewis , I guess you can ignore this. :slight_smile:

2 Likes

Except...

It's the prefix we're talking about, and that isn't used in the JSON key itself. There are also no case-sensitivity issues with the token, since KM variables aren't case-sensitive.

But the token is problematic in other ways -- consider the following:

...where our two separate KM variables are reduced to a single JSON key/value pair.

And while you can see from the above that it treats the space character at the end of the prefix as optional, it doesn't do the same for the (commonly used) underscore.

So I think I'd only use this token for variables whose names I was in control of...

I hear you. In my case, I want to call a sub-macro, but have it still have access to my local variables, and even be able to update them.

So I pass local variables in a JSON string. The sub-macro does whatever it does with them, even changing them. The sub-macro returns the updated local variables in a JSON string, and I use "Set Variables to JSON" with an empty prefix to update them in the calling macro.

It seems to work OK, now that I'm using JXA to get the local variables.

This is the code I'm using:

const result = {};
Object.keys(kmvar).forEach(key => {
	if (key.toLowerCase().startsWith("local"))
		result[key] = kmvar[key];
});
return JSON.stringify(result, null, 2);

(And yes, I could do this with a "reduce", but to my old-school mind, this is easier to understand.)

It's in an "Execute JavaScript with Automation" action with "Modern Syntax" and "Include all variables".

Hope that makes sense.

1 Like

lowering in the JS is much cleaner than the way I was looking at -- re-casing the variable name itself. It works, but I'd worry about persistence and side-effects:

image

Demo: Re-Case Variable Names.kmmacros (5.9 KB)

And, of course, just using that routine adds a couple of unwanted local variables into your JSON! Probably best avoided...

You can always delete the variables after you use them, but they'll still be in the "accessed variables" list.

1 Like

So, net result, be careful with the JSONFromVariables token.

That said, I think Dan is right, variables are case insensitive and thus the prefix should be case insensitive, but the result will be JSON, which will use the case sensitive name of the remainder of the variable.

Variables do have a case preserving name, but even so it's problematic.

So given the issues, despite thinking it should probably be case insensitive, I'm going to leave it alone and just document it.

3 Likes

You echoed my thinking exactly. Thanks!

1 Like