Variables from JSON
Turns each top-level key of a JSON object into its own variable.
Make variables from json
Keyboard Maestro: SetVariablesToJSON
Details #
Reads a JSON object and sets one variable per top-level key, so {"name": "Ada", "age": 36} gives you {{name}} and {{age}}. Values keep their type: numbers stay numbers, and nested objects and lists stay as JSON values you can reach with the json function.
If the JSON is not an object (a list, text, a number), the whole value goes into a single variable called prefix, or json when there is no prefix.
The output is the number of variables set. The trace notes "set N variables".
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
json | text | The JSON text, or a value that is already a list or object. Required in practice: with nothing, the step fails. (text is another spelling.) | ||
prefix | text | Text put in front of every variable name, such as user_ giving user_name. | ||
text | text | Another spelling of json. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
The dry run simulates this step, so it does not set the variables there. For real it does:
- Make variables from {"name": "Ada", "city": "London"}
- Show “{{user_name}} lives in {{user_city}}” for 3 s
Gotchas #
Invalid JSON text fails with "that text isn't valid JSON".
Only top-level keys become variables. Nested values stay inside their variable.
A key that matches an existing variable overwrites it, which is why a
prefixis a good habit.This step is simulated in a dry run, so later steps that read these variables see nothing there.