Brian Dunning's FileMaker Custom Functions

SetVariableFromJSON ( json )

Set the variable(s) using the JSON root keys as names

  Average rating: 5.0 (1 vote) Log in to vote

Andrea Niro   Andrea Niro - Show more from this author

Share on Facebook Share on Twitter

  Sample input:
{
"GLOBAL_JSON" :
{
"a" : "b",
"c" : "d"
},
"array" : [ "index 0", 1 ],
"variable" : "value"
}
  Sample output:
$$GLOBAL_JSON = { "a" : "b", "c" : "d" }

$array = [ "index 0", 1 ]

$variable = value

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

IsValidJSON ( json ) is a custom function that evaluates the result of this formula:
not ( Left ( JSONFormatElements ( json ); 1 ) = "?" )

--------------------------------------------------------------------------------------

The function sets a variable as global based on my own naming convention:
- local variables start with a lowercase latin letter and each subsequent word uses a Proper notation (e.g. $thisIsAVariable)
- global variables are always written UPPERCASE (e.g. $$VARIABLE)

--------------------------------------------------------------------------------------

The $### variable drastically improves performance: while using a temporary variable to store the data and evaluating the content in the next steps forces FileMaker to pack (Quote() function) and unpack (Evaluate() function) the information, using the variable bypasses this process, speeding up considerably the calculation while working with large amount of data (e.g. lists)
The name of the variable itself is arbitrary: I just use a name that's unlikely/impossible for a variable.

--------------------------------------------------------------------------------------

The function does consider only the root's keys because it's often used to create a variable then used to pass a variable to a subscript.
e.g. using the function to elaborate this JSON in a script
{
"parameter1" : "value",
"parameter2" :
{
"parameter3" : "value"
}
},
gives two variables ($parameter1 and $parameter2), where $parameter2 can be directly passed to a subscript, which then can use the function again to initialize a local variable $parameter3

 

Comments

Mark D Kerr   Mark D Kerr, Discovery Reef Design & Consulting
Jul 2, 2021
Well done! I started off to write this myself this morning. It solves a serious issue that the recursive functions doing the same task all suffer from. When a JSON object contains an encoded image, large images crash the function. Your use of the While statement does not.
 
Mark D Kerr   Mark D Kerr, Discovery Reef Design & Consulting
Jul 2, 2021
Head to head with the recursive functions, this runs about 80 - 400 times faster, depending on the number and size of the images.
 

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: