Browse the docs

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 #

ParameterTypeRequiredValuesWhat it does
jsontextThe JSON text, or a value that is already a list or object. Required in practice: with nothing, the step fails. (text is another spelling.)
prefixtextText put in front of every variable name, such as user_ giving user_name.
texttextAnother 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:

Split a JSON object into variables
Do
  1. Make variables from {"name": "Ada", "city": "London"}
  2. Show “{{user_name}} lives in {{user_city}}” for 3 s
Open in Switchboard to try it

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 prefix is a good habit.

  • This step is simulated in a dry run, so later steps that read these variables see nothing there.

See also #

Set variable, Send HTTP request, json