FileMaker 16: Passing Multiple Parameters Natively Using JSON

  • 5

FileMaker 16: Passing Multiple Parameters Natively Using JSON

Tags : 

With the release of version 16, FileMaker developers finally have the ability to natively pass and parse multiple script parameters . This is made possible by the new JSON functions — specifically, the JSONSetElement function and the JSONGetElement function. There are various methods and syntaxes FileMaker developers have been using for passing multiple parameters to and from scripts, but the most common one I’ve come across is key-value pairs in the format “key1 = value1 ; key2 = value2 ; key3 = value3” etc. This is a nice simple format, but it has some limitations. The new method in 16 is to pass a JSON object, with a series of elements that form the key-value pairs, and parse those elements out of the JSON object in the sub script. So, what once was passed as:

"id = 32313 ;
name = alice ;
address = 123 Main St. ;
member = true"

is now passed as:

{ 
     "id" : 32313, 
     "name" : "alice", 
     "address" : "123 Main St.", 
     "member" : true 
}

And on the receiving end, we parse out each element into a variable and use them from there, using the JSONGetElement function. There’s no longer a need for custom parsing functions, or complex let statements that parse and instantiate each variable.


There are two ways to form the JSON object when passing the parameters. The first, and more clunky way, is to just write the JSON from scratch. I say clunky because you have to do things like escape lots of quotation marks, and insert ampersands, and insert unescaped quotation marks, and insert syntax errors, and (barf) and I wouldn’t recommend it. But this is what you’d write, if you so desired:

"{ 
     \"id\" : " & TableName::ID_Field & ", 
     \"name\" : \"" & TabelName::Name_Field & "\", 
     \"address\" : \"123 Main St.\", 
     \"member\" : true 
}"

 

The second, and more elegant way, is to leverage the JSONSetElement function:

JSONSetElement ( "{}" ;
     [ "id" ; TableName::ID_Field ; "" ] ; 
     [ "name" ; TableName::Name_Field ; "" ] ; 
     [ "address" ; "123 Main St." ; "" ] ; 
     [ "member" ; True ; "" ] 
)

Now when the script into which you’re passing parameters begins running, you (optionally) set a variable to*:

Set Variable [ $param ; Value : JSONFormatElements ( Get ( ScriptParameter ) ) ]

And in the following script steps, you instantiate a variable for each of the incoming JSON elements as needed, using the JSONGetElement function. Since the variable $param is now just a tidy little package of JSON, you can pull out individual elements from it like so:

Set Variable [ $id ; Value: JSONGetElement ( $param ; "id" ) ]
Set Variable [ $name ; Value: JSONGetElement ( $param ; "name" ) ]
Set Variable [ $address ; Value: JSONGetElement ( $param ; "address" ) ]
Set Variable [ $member ; Value: JSONGetElement ( $param ; "member" ) ]

It works beautifully. This is JSON functionality at its most basic, but it’s a pleasure to use, and provides us with a brand-new, simple way to do something we’ve wanted to be able to do natively for years. And what’s cool about the new JSON functions is that FileMaker developers can start natively doing even more complex things, like building arrays of data, with child records and all, right inside variables, and pack and unpack them at will using native functions. All in all, JSON is a welcomed addition to the FileMaker platform’s toolset.

 

*The JSONFormatElements function is nice to have for looking at parameters in the data viewer, but is not actually required. All it does is take the JSON that is passed in and format it neatly with line breaks and indentation. You can just use Get ( ScriptParameter ) alone and everything will work just fine.


5 Comments

Fabio

May 11, 2017at 6:59 am

Great and useful article! Thanks for sharing.

Dominick Capobianco

May 12, 2017at 1:36 pm

Excellent. Great examples too. Thank you !!!

John Mark Osborne

May 12, 2017at 2:08 pm

Awesome!

Susan Prosser

May 12, 2017at 4:32 pm

Nice! Thanks for writing this up.

Jeff Drake

May 13, 2017at 6:59 pm

Great idea. Thanks for a new option!

Leave a Reply

two × 2 =