FileMaker 16: Start Using JSON Right NOW

By: Jerry Salem, FileMaker Consultant

FileMaker has always been able to integrate nicely with other technologies, but with FileMaker 16 it's even better.  

One of the flagship features of FileMaker 16 is the introduction of JSON.  This is a way to structure data to make it easy to share with other platforms.  Remember XML?  This is similar to that, it’s a way to represent data so that other systems can utilize the data. 

Luckily, you can use JSON right away, even in standalone databases that don’t communicate with other platforms.  Thanks to the way it was implemented, we now can use JSON to solve a long-standing issue in FileMaker, making the ability to pass parameters between scripts more robust.  When running a script, you can give the script a parameter to instruct it on what action to take.  For example, you can have two buttons on a layout, one to view a particular report sorted one way, and the other to sort it another way. 

One issue that has been around since the introduction of script parameters, is how to pass more than one parameter to a script.  So let’s go ahead and improve the report script.  For this example, let’s say you want to sort a report, but you also want your end user to be able to show a detail report or a summary report.  This would require passing more than one parameter to the script.  In this example we would want to pass one script parameter to describe the sort, and another for the view.  Unfortunately FileMaker does not offer a way to pass more than one parameter to a script. 

In the past there have been two ways to achieve this.  One way is to send the script parameter as a ‘return delineated list’, then parse out the sort and view values using the ‘get value’ script step.   

Name 
Summary
 

This will work, if you can guarantee that there will not be any carriage returns in your list, that you send the values in the right order, and that you’ve accounted for empty values. 

I have seen developers pass multiple words as parameters, such as starting environment, primary key, and sort type (ex. Invoices 123 Date) then use the ‘LeftWords’, ‘MiddleWords’ and ‘RightWords’ functions. 

Another way to accomplish this would be to use a pair of custom functions to package the parameters in a way that we can unpack them in the script.  There are a couple of functions available for this, including ‘Modular FileMaker’.  My favorite is a pair of custom functions that I found on the very old SixFriedRice site (http://sixfriedrice.com/wp/passing-multiple-parameters-to-scripts-advanced/). This ‘PassParameter’ function wraps parameters in name/value pairs. Kudos to SixFriedRice for keeping the site up after all these years. 

These are packaged like this:

<:sort:=name:><:view:=summary:>   

Then use the other custom function (getDict) to deconstruct the values out based on their names (Sort and View in this case). 

With FileMaker 16 we don’t need to resort to using a custom function.  We can now use an industry standard in JSON to pass parameters to scripts.  This has the advantage of using built-in functions and gets us familiar with using JSON functions.  To encode script parameters use the new ‘JSONSetElement’ function. 

To pass parameters like we’ve described here, use this function.  Since we are encoding two parameters, just contaminate them: 

JSONSetElement ( $test ; ["sort" ; "Name" ; "text"];[ "view" ; "Summary" ; "text"] ) 

To decode the script parameter you now use the JSONGetElement function like this: 

JSONGetElement ( $test ; "view" ) 

Using the built-in functions you can start learning the ins and outs of JSON.  It also allows you to jettison custom functions.  But, there are a couple of gotchas to be aware of before you dive in.  FileMaker’s native fields and variables are not case sensitive.  However, like ExecuteSQL, JSON is case sensitive.  So my old saying ‘treat everything as case sensitive’ is true more than ever.  In the above example, calling JSONGetElement($test;”VIEW”) will yield a null string compared to the way it is written above.

Another thing to watch out for is the first argument in the JSONSetElement function.  The first time you set a variable, you can leave the JSON parameter blank.  If you call one of the other ‘Set’ functions you have to make sure to include the JSON variable.  So even though it looks a bit redundant, you should set variables like this: 

SetVariable[$Parameter; Value JSONSetElement ($Parameter; “sort”; system::gSort ; “text”) 

The final parameter, text in this case, designates the type of data encoded.  Since FileMaker treats variables all as text, you should always be set to text or 1.  When using JSON in cURL statements it will be useful to designate data elements by their proper types. 

While this isn’t something that you can’t do already using a pair of custom functions, it is a great way to get your toes wet before it becomes a major feature in FileMaker.  Plus using the built-in function brings uniformity across different solutions.  Have Fun! 

ITS Email Newsletter Archive

© 2018 IT Solutions Consulting, Inc. All rights reserved. Privacy Statement
IT Solutions
Top