JSON Parameter Passing

Thank goodness JSON was implemented within FileMaker. Without it, we would still be using a variety of other methods for handling multiple parameters within a FileMaker script. Using JSON, we now get the double-sided benefit of 1) having an efficient serialization of key/value pairs as inbound parameters and 2) it just happens to be the standard which most of the rest of world is using for a large chunk of how things interoperate.

In this video, and the technique file, I'll showcase a few examples of how you can receive and work with inbound parameters when using JSON as the method for handling them. We discuss script results, clean-reading code and the final example provides a very useful custom function for the auto-instantiation of JSON-based script parameters into their FileMaker scripting counterparts.

Using the provided JSONVariables custom function is a great alternative to individually parsing all of the inbound parameters. It doesn't replace the need to initially declare your variables, but it does provide the opportunity to have cleaner reading self-documenting code. If you've not used JSON for handling your parameters before, then you should start right now and use this video and file as guidance!

AttachmentSize
JSONParameterPassing.zip1.65 MB

Comments

Hi Mat, thanks for another great video, I have been with FMP for decades but always find something new within your videos, its always been well worth the subscription.

When working in FMP and in particular with JSON, just thought I might mention that I tend to define custom functions to serve as keys into JSON elements, I haven't seen this topic mentioned elsewhere so thought I would mention it here.

In the interests of DRY code and the avoidance of errors I define CF's to act as constants and in particular JSON keys like this:
e.g. KEY_ACTION (which simply returns "action_key") or KEY_REQUEST which returns "request_key".

Then in the JSON we can have JSONSetElement ( "{}" ; [KEY_ACTION ; "do stuff"; JSONString] ; [KEY_REQUEST ; "a request" ; JSONString]) for instance.

Later we can use JSONGetElement ($json ; KEY_ACTION) or JSONGetElement ($json ; KEY_REQUEST) etc to retrieve the data. This completely avoids the scenario of mis-typing the key and nulling the data thereby breaking your code. FMP also auto-completes the key as you type it in which is convenient. This may not work with your CF for auto creating variables in this video (not examined this as yet) but is very useful elsewhere in a given code base.

I also tend to define CF's for any other constants I might need such as Layout Ids, Table Ids etc, I always define a CF to replace a hard coded string or numerical value / constant wherever possible. If you have covered this elsewhere and I am stating the obvious, apologies but I have not come across this topic mentioned anywhere so far!

Kind regards

Bob Avery