Using JSON to Pass Parameters to a Sub-Macro

REQUIRES KM version 9

Purpose

This post attempts to show how the new JSON features in KM v9 can be used to pass parameters to a sub-macro.

The JSON features in KM v9 are incredibly useful, and I can only imagine how we'll expand on their usage over time. @peternlewis :heart: :bowing_man:

I also show how to use Local variables, because they're awesome beyond words, and I have no idea how I missed them until recently.

Version History

2019-08-29 12:00 PDT: Initial release (The time is an approximation.)

JSON Parameters - Sub-Macro.kmmacros.zip (2.7 KB)

Notes

  • I'm not trying to create any standards here - just showing some options.
  • I know some of you hate long variable names. I'm making them long just so they're clear in this example.
  • If you don't know what "local" variables are, here's a quick primer:
    • Variables whose names start with "Local_" are, well, local.
    • Local variables only exist inside the macro they're defined in.
    • They automatically get deleted when the macro is done, which means no more having to clean up variables!!!
    • They don't have any value until you set them, which means you don't have to initialize them beforehand.
    • Although the variables themselves only exist inside the macro, you can pass their contents to another macro.
    • There's also Instance variables, but that's not within the scope of this post.

Premise

  • The sub-macro JSON Parameters - Sub-Macro expects TriggerValue to contain a JSON string with specific fields.
  • It pulls out the fields and displays their values. It also optionally validates the fields.

JSON Parameters - Sub-Macro


Example 1: Pretty JSON String

Result:

First Name:		Maynard
Middle Name:	G.
Last Name:		Krebbs

Example 2: Compact JSON String

Result:

First Name:		Maynard
Middle Name:	G
Last Name:		Krebbs

Example 3: Set JSON Value' Action

Result:

First Name:		Maynard
Middle Name:	G
Last Name:		Krebbs

Example 4: Optional 'Validate' Parameter, no errors

Result:

First Name:		Maynard
Middle Name:	G.
Last Name:		Krebbs

No Errors.

Example 5: Optional 'Validate' Parameter, Errors

Result:

First Name:		
Middle Name:		
Last Name:		

Error Message(s):

First Name is required
Last Name is required

8 Likes

Amen ...

These new features allow, amongst other things, very rich interactions between JS script-action-generated JSON and other actions, including For ... Each Actions iterating through the parts of a JSON array or dictionary.

4 Likes