In an Execute Javascript for Automation action, you can use JSON.parse to read a json-structured KM variable value, and create other KM variables from the parse result.
JS source:
(() => {
'use strict';
const main = () => {
const
ks = ['id', 'name', 'homepage', 'twitter_screen_name'],
pfx = 'json_',
kme = Application('Keyboard Maestro Engine'),
result = bindLR(
(() => {
try {
return Right(JSON.parse(
kme.getvariable('rawJSON')
))
} catch (e) {
return Left(e.message)
}
})(),
dct => Right(ks.map(
k => {
const
v = dct[k],
strValue = Boolean(v) ? v.toString() : 'null',
kmName = pfx + k;
return (
// Effect
kme.setvariable(kmName, {
to: strValue
}),
// Value
kmName + ' -> ' + strValue
);
}
).join('\n'))
);
return result.Left || result.Right;
};
// GENERIC FUNCTIONS ----------------------------
// Left :: a -> Either a b
const Left = x => ({
type: 'Either',
Left: x
});
// Right :: b -> Either a b
const Right = x => ({
type: 'Either',
Right: x
});
// bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
const bindLR = (m, mf) =>
undefined !== m.Left ? (
m
) : mf(m.Right);
// MAIN ---
return main();
})();
Thank you very much ComplexPoint. Getting an answer from a legend like you is amazing. I should invest some time learning JS. The automation possibilities look almost infinite.
Actually, I decided to save the first key's values to a Keyboard Maestro variable and then parsed it further down. Would still be a nice-to-know if I can do this without an intermediate variable.