'Set Variables with Prefix' action renames variable prefix on it's own

After installing Keyboard Maestro on a second computer with syncing enabled, I noticed a daily macro was missing information that comes from variables in the output. It turns out that 'Set Variables with Prefix' is behaving strangely.

I have it set variables with a prefix of "JSON" in the action, but it creates variables called "JSON1" instead.

Here is a screenshot of the macro.

I can update the references to the variable, but I am curious why it isn't using the variable name I specified.

For searching the forums, it appears variable aren't synced, so there isn't a change of a collision in namespaces since each system has it's own set of variables.

Still, my best guess for the root cause is that KM thinks the variable name "JSON" is taken and auto increments it to avoid overwriting an existing variable. There isn't a variable by that name. I tried creating the variable "JSON" with a default value to see if KM would use it , but it continues to use "JSON1"

Ideas? As a work around, I'll change variable references to use the new variable name, but I'd like to see if there is a fix for this behavior.

Update: The issue is more complicated, so I'll do more troubleshooting after I get off of work tonight to look at it. It's not just the variable name prefix that is the issue, but it's no longer parsing the JSON correctly and creating JSON* variables like it was before .

Plus changing the variable name from JSON to JSON1 in the set variable macro caused it to create a new JSON11 variable. Anyway, I'll look at this tonight(MT). If you have seen this behavior before, feel free to post ideas on how to fix it.

When you post again, if you still need help, please include some sample JSON that shows the problem. Thanks.

1 Like

I was trying to figure out how to do that without including sensitive information, which unfortunately is included in the results.

However, I did figure out the issue. Apparently, the return data format changed. Originally it was a JSON dictionary, but for some reason the JSON is now enclosed in square brackets.

so { "firstname": "Joe", "lastname": "Smith"}
[{ "firstname": "Joe", "lastname": "Smith"} ]

I solved it by using jq cmd line utility to extract the dictionary from the array/list.

eg Shell macro looks like - curl https://some/url/parameters | /usr/local/bin/jq '.[0]'

That did the trick.

Now why the return data format changed...that I will probably never know.

Because the returned data is an array of one entry, Keyboard Maestro is using the name "JSON" followed by the index of the array "1" for the first entry.

If you used something like %JSONValue%AW_OUTPUT[1]% to get the first entry, then you should get back to what you wanted. But presumably there is a reason it is now an array, maybe the server can now return more than one entry, in which case you might have to deal with that.

EDIT: I didn't notice Peter's response before I posted this. In any case, my response adds a little more information.

That format is an array. So if you're going to assume there's only one entry in it (or at least you only care about the firs entry), you can use KM's JSON array syntax to access it:


as in:


Or you could use this regex to remove the enclosing square brackets:


as in: