Parse JSON Macro (v9.2)… is there a better way?

I've been capturing a series of variables by searching a tab-delimited text file with RegEx. It works, but takes several seconds. After learning how to convert the file to JSON I came up with this, thanks to several posts on the forum. It captures the variables almost instantly. My question is, is there a more efficient way to build the macro? It seems creating a separate "Save Variable" action for each variable in a dictionary is a little tedious and since the variable names match the labels in the JSON I feel there may be a more direct method.

The sample JSON is only a small subsection of the actual JSON which contains over 100 dictionaries.

Any suggestions are welcome.

Parse JSON Macro (v9.2)

Parse JSON.kmmacros (12 KB)

Hi @rcraighead,

Just by reading your post, I think you can do this:

  1. set a variable into text: get all JSON data you want to retrieve and connect these data with a unique string.
    E.g., Set Local__myVar to:

  2. Search that variable with RegEx and save them into the variables. See the example here given by @JMichaelTX :

That surprises me. I have used this method in a number of my macros and it has always been fast. Of course, it depends on the quantity of your data, and complexity of required RegEx patterns. If you want to post a real-world example of your source data, data extraction rules, and desired result, I'll take a look. If you have a lot of data, please zip the file and post that.

It looks like you are creating hundreds of KM Variables. I wonder how you plan to use all of these variables?

While I am a huge fan of KM, and use it many times a day, IMO if you need to deal with a lot of object data, KM is not well suited for it. I have found that JavaScript is much much better. As you might guess, it easily saves arrays and objects to JSON strings, and then easily recreates same from the JSON string. Referring to array elements and object keys is very easy in JavaScript. And one you have these in JavaScript, it is easy to use the many functions of JavaScript to process your data.

To be clear, by "JavaScript" here I mean JavaScript for Automation (JXA).
Hope this helps.

Thanks for the responses. The RegEx solution has worked very well since @JMichaelTX shared it with me. But in this early test I found what takes RegEx 5 sec. is done in less than 1 sec. using my current JSON method.

The purpose of the variables is to populate product templates in Adobe Illustrator. There are 100+ products sharing about 20 variables. A JSX (Adobe's flavor of Javascript) function library is pre-loaded in Illustrator and functions are called through KM like this:

I'm very open to learning JXA. I just have not found sufficient resources for learning it. Most of my scripting knowledge has come through the Adobe Forum and JXA does not seem to have much of a following there.

Hi @rcraighead, I'm learned all the scripting languages myself, using only the online resources, so I might be talking about things that I know not. But as far as I see it, if we are not dealing interacting with programs and we just want to process some text, JXA works just as JavaScript, except that we need first to get the text into the JXA variables.

For instance, this code gets the text in the SystemClipboard and save it to myVar:

app = Application('System Events');  
app.includeStandardAdditions = true;  
var myVar = app.theClipboard(); 

Another example:

app = Application('System Events');  
app.includeStandardAdditions = true;  
var kme = Application("Keyboard Maestro Engine");
var myVar = kme.getvariable("Var")

This code get the value of the variable Var in KM to myVar in JXA.

Now, we can process myVar using the language in JavaScript.

Correct me if I'm mistaken.

Thank you @martin. I'll use your examples to study more on JXA.
I DO interact extensively with Adobe Illustrator and maintain a Javascript Function library of over 200 functions, all of which are called directly or indirectly from KM. It works and it's saved our artists thousands of hours. If I could simplify the development process I'd be thrilled.

1 Like

Both JXA and Adobe JSX share the same common core of JavaScript. JXA simply adds the ability to interact with other apps and the macOS. You may or may not need this with Adobe.

IAC, you may be able to use only JSX to:

  1. Read a file that contains a JSON string.
  2. Parse this string into JavaScript objects and arrays in JS Variables.
  3. Use these JS variables to set the Adobe fields using JSX.

I don't any advantage to using KM Variables, but then I have only a very limited understanding of your complete workflow.

BTW, if you want to learn more about JXA, see my Github Gist on JXA Resources.

1 Like

Thank you for the tips and the link. I will dig into learning JXA.

Edit: It is these kind of posts that scare me away from JXA. I know this is only one person, but I don't understand why few Adobe scripters seem to embrace JXA.

You just have to learn to ignore people with extreme views. The user, "foo", in the Answer is well known for being highly critical of JXA. Just ignore him. JXA, like any language, is not perfect, but it works very well for the most part.

1 Like

JXA is just not well known or received in the Adobe Forum:

If I understood how to use JXA along with my existing JSX Functions Library it may be worth more study. Just not getting anywhere. Perhaps there are some Adobe users in this forum who have some success stories.

When writing JavaScript code for use INSIDE of Adobe apps, then use JSX.
I have NOT used JSX, but my expectation is that it uses the same core JavaScript that is used for both JXA and web-based JavaScript.

You only need JXA when you want to access other apps or the macOS.

So, if you have a JSON string in JSX, then you can create JavaScript objects from it using this simple JavaScript function: JSON.parse() - JavaScript | MDN.

There was a very determined and insistent spate of that after someone felt aggrieved that their offering had not been accepted by the mac Automation unit at Apple :slight_smile:

The Automation library marketed as JS for Automation/JXA etc, (and embedded in a JSContext instance of the Safari JavaScript interpreter) is perfectly serviceable.

The only thing it never implemented before Apple (quite understandably) closed that automation unit was location specifiers for collections.

But more generally, it's just a standard JS interpreter, plus an instance of this library:

1 Like