Brian Dunning's FileMaker Custom Functions

jsonToVars ( JSON ; scope )

Converts JSON into FileMaker variables

  Average rating: 4.5 (2 votes) Log in to vote

Rob Poelking   Rob Poelking - Show more from this author
Kiza Solutions
https://kizasolutions.com

Share on Facebook Share on Twitter

  Sample input:
jsonToVars ( JSONSetElement ( "" ; "foo" ; "bar" ; "" ) ; "local" )
  Sample output:
VOID ($foo = "bar")

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

NOTE: Requires FileMaker 18 or above
To be used when passing parameters to a FileMaker script. This function will convert a JSON object into key value pairs using the JSON keys as variable names and the associated values assigned to them. The function itself will not return anything but when instantiated in a script, will render the variables needed by the script.

Specify local or global for variable scope.

For example, attach a button to a script and pass in the parameter JSONSetElement ( "" ; "foo" ; "bar" ; "" ) . In the script assign a dummy variable -- Set Variable [ $vars ; jsonToVars ( Get ( ScriptParameter ) ; "local" )]. The variable $foo will be assigned "bar" and can be used in the script. Change the scope to "global" to assign $$foo to "bar".

 

Comments

Kyle Williams   Kyle Williams, Augusto Digital
Aug 14, 2020
I changed the "scope" parameter to "Globals" (as a boolean value) and then found that I could use a let function to get the sub JSON arrays from the variables this function creates which works really well:

My script creates a custom dialog window, this is my formula to extract all of the json elements as variables:
Let([
vars = jsonToVars ( Get(ScriptParameter); 0 )
;vars = jsonToVars ( $window; 0 )
;vars = jsonToVars ( $position, 0 )
];"")
 
Steve Thoms   Steve Thoms, International Plastics
Jul 19, 2021
I recently began using this and tried it on a scheduled script run. I kept getting 1204 error until I changed the call to this:
This is the parameter I am sending: JSONSetElement ( "" ; [ "the_date" ; Get ( CurrentDate ) ; JSONString ] )

And this is the first step in the called script:

JSONToVars ( If ( isServer ; Evaluate ( Get ( ScriptParameter ) ) ; Get ( ScriptParameter ) ) ; "" )

Since the schedule passes the parameter as pure text, you need to evaluate it for the magic to happen.

Hope that helps someone,

Steve
 
Rob Poelking   Rob Poelking, Kiza Solutions
Jul 19, 2021
Thanks for the tip Steve!
 
Scott Newton   Scott Newton, Integration Consultants
Sep 29, 2024
Very helpful, but found a small flaw when using in a loop context: Any null values in the JSON object will not clear any existing $var values. So if one iteration had a value in JSON for "MiddleName", but the next one had no value here, the value for $MiddleName persists rather than clears. I can get around this by formally clearing possible $var's each loop, but would love to have the function do the heavy lifting. I might tackle this myself, but wanted to note this behavior.
 
Rob Poelking   Rob Poelking, Kiza Solutions
Sep 30, 2024
Thank you, Scott. I've updated the function to nullify a variable if no value is supplied in the JSON.
 
Scott Newton   Scott Newton, Integration Consultants
Sep 30, 2024
Very helpful, but found a small flaw when using in a loop context: Any null values in the JSON object will not clear any existing $var values. So if one iteration had a value in JSON for "MiddleName", but the next one had no value here, the value for $MiddleName persists rather than clears. I can get around this by formally clearing possible $var's each loop, but would love to have the function do the heavy lifting. I might tackle this myself, but wanted to note this behavior.
 
Scott Newton   Scott Newton, Integration Consultants
Sep 30, 2024
@Rob Poelking - Wow, thank you so much!
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: