To Top

FileMaker Pro: Extracting Multiple JSON Formatted Script Parameters In One Step

Set Variable

Let's assume for the rest of this article that we're clicking a button on a layout which is performing some action, while passing in the following script parameters. Note this is a single level JSON object and that this technique does not currently support JSON with any kind of hierarchy.

JSONSetElement ( "{}" ;

[ "action" ; "edit_record" ; JSONString ] ;

[ "create_log" ; 1 ; JSONString ] ;

[ "id" ; "123456" ; JSONString ]

)

One way of retrieving these values and getting them into variables for the script to use is this.

# Get JSON formatted script parameter
Set Variable[ $SP; Value:JSONFormatElements ( Get ( ScriptParameter ) )]

# Set variables
Set Variable[ $action; Value:JSONGetElement ( $SP ; "action" )]
Set Variable[ $create_log; Value:JSONGetElement ( $SP ; "create_log" )]
Set Variable[ $id; Value:JSONGetElement ( $SP ; "id" )]

There's nothing wrong with this approach, but if you have ten or more parameters coming into the script, it's going to take a few minutes to code all those Set Variable steps. Additionally, when we make another script, we can't likely reuse the above because the parameters will probably be named differently.

Extracting The Parameters In One Script Step

So how can we be smarter and save ourselves development time? Well, one approach would be to set up a loop in the script and iterate through the JSON setting variables. This would end up being quite a few lines of code. But, in a similar way we can use the While function introduced in FileMaker 18 to iterate through the JSON. A quick word on the While function.

While ( [ initialVariable ] ; condition ; [ logic ] ; result )
  • The "initialVariable" is where we set up the starting values that the rest of the While function will use.
  • The "condition" determines if we should continue to loop.
  • The "logic" is the set of actions that take place during each loop.
  • The "result" is the returned value when we exit the loop, if required.

There's one more tricky thing we need to account for, and that is that we can't use Set Variable with a dynamic variable name. So we'll use some Evaluate and Substitute sorcery to get around this, similar to a Support Group technique from way back in 2010.

So this is our replacement code for the mutiple Set Variable steps above.

# Get JSON formatted script parameter
Set Variable[ $SP; Value:JSONFormatElements ( Get ( ScriptParameter ) )]
# Extract all parameters into variables
Set Variable[ $extract_parameters; Value:
// Set up the While function
While (
// Create our initial variables [
// Get the list of JSON keys keys = JSONListKeys ( $SP ; "" ) ;
// Count the number of keys we have to determine when to exit the loop key_count = ValueCount ( keys ) ;
// Create our counter used for counting the iteration we're on counter = 1 ] ;
// Set our condition under which we continue to loop - while the counter is less than or equal to the key count, perform the logic counter ≤ key_count ;
// Set up the logic that is performed on each loop [
// Get the name of the parameter parameter_name = GetValue ( keys ; counter ) ;
// Create our variable name variable_name = "$" & parameter_name ;
// The value of the parameter will be retrieved in the formula section below
// Set up the text string with the formula we will use to make the variable formula = "Let ( variable_name = JSONGetElement ( $SP ; parameter_name ) ; \"\" )" ;
// Create the variable by substituting our formula with our data above, and then evaluate the expression create_variable = Evaluate ( Substitute ( formula ; [ "variable_name" ; variable_name] ; [ "parameter_name" ; Quote ( parameter_name ) ] ) // Close Substitute ) ; // close Evaluate
// Iterate the counter and loop counter = counter + 1 ] ; // Return nothing as we don't need a result in this instance, the variables have been created already
"" ) ]

Not only have we created all three variables in just one step, this code is completely independent of any context, so it can be reused in any script to extract the script parameters into variables.

Download

There's a basic example file that you can download here: Get Example File

Conclusion

As mentioned above, the While function was introduced in FileMaker 18, so this technique is only compatible with that version and higher. It is possible to do something similar using a script loop if you're using version 16 or 17 - contact us if you're interested in that. It's worth noting that as this technique eliminates the Set Variable steps, it's probably a good idea to note at the top of your script in a comment line what parameters this script expects so you have that to reference.

If you need help with your FileMaker app, or want to save money on your licensing of Claris products, contact us and we'll be glad to assist. And if you're not keeping an eye on your FileMaker Server, you should be! Consider signing up for OverwatchFM, our FileMaker Server monitoring service, and get alerts on storage space issues, backups failing, low memory, process crashes and more.

Update 2020–07–09:

Huge thanks to Russell Watson for pointing out that the original version of this technique converted numbers to strings. The revised version above and example file now reflect his contribution to the technique.