Parsing Methods for JSON Parameters

  • 0

Parsing Methods for JSON Parameters

Tags : 

Here at AppWorks we’ve been revamping some of our popular (and free!) downloadable modules with FileMaker’s native JSON functionality. But why stop there? The more JSON the better.

Why do we love JSON so much?

  • It’s human-readable and easy to write
  • It’s lightweight and flexible
  • It’ll make your API Integrations even easier to implement
  • It makes multiple parameter passing more robust and reliable

The benefits of developing with JSON and its applications are endless, but I’ll be focusing on my personal favorite – script parameters. Before native JSON functionality was released, many tricks and workarounds were used to pass multiple parameters into a script using the single parameter field we are given. Some were more reliable than others, but overall this was not ideal.

With JSON we can leverage it’s lightweight and flexible syntax to reliably pass a set of parameters that can be as simple or complex as needed. If you’re like me however, you may find parsing JSON parameters is a bit tedious. To help alleviate some of the repetitive code required to parse out each JSON element into variables, I’ve created two methods to handle the parsing. Both of these methods use a ‘Let’ statement along with the ‘Evaluate’ function and can easily be copied into any script.

Note: The parsing methods below only parse the top-level JSON keys.

Method 1: Scripted Loop

Using only eight lines of code we can quickly loop through a JSON object to instantiate as many variables as we need. Although this may seem like a lot of code to parse only two or three variables, it’s true value is in it’s reusability and flexibility. These script steps can be copied/pasted into EVERY script and work, and it will continue to work without any extra scripting when two variables suddenly become ten. The screenshot below will show you how to setup your loop.

The variable called $parse is where the local variables are created:

Let ( [

list = JSONListKeys ( $raw ; “” ) ;

key = GetValue ( list ; $i ) ;

value = JSONGetElement ( $raw ; key ) ;

error = “$error=\”Invalid JSON\”” ;

JSONerror = Case (Left(JSONFormatElements($raw) ; 1 ) = “?” ; 1 ; 0)

];

Case (

     //Error was found

     JSONerror = 1 ;

     Evaluate(“Let ( ” & error &  ” ; \”\” )” ) ;

 

     //Create local variable

     Let ( eval = Evaluate(“Let ( [ $” & key &  “=\”” & value & “\”] ; \”\” )” ) ; “” )

) //end Case statement

) //end Let statement

Method 2: Custom Function

After writing my own custom function I decided to search the FileMaker community for one because surely someone else MUST have created one already. There may be more, but I was able to find at least one published by Mislav Kos of Soliant Consulting. It can be found on Brian Dunning’s FileMaker Custom Functions website here.

The benefit of the custom function is that my eight lines of code from the scripted loop turned into a single line. So while custom functions are more difficult to debug, I personally enjoy watching a ton of variables being created in a single script step. So without further ado, here it is:

/*

JPP ( json )

PARAMETERS:

json – JSON object

PURPOSE:

Creates local variables for each top-level JSON key.

OUTPUT:

Local variables created from the top-level keys of the JSON object

Errors will produce a variable called $error when the JSON is invalid

and a variable called $raw with the raw text.

HISTORY:

Created on 2018-10-31 by Michelle Davison

*/

 

Let ( [

list = JSONListKeys ( json; “” ) ;

key = GetValue ( list ; ValueCount(list) ) ;

value = JSONGetElement ( json; key ) ;

raw = “$raw=\”” & json & “\”” ;

error = “$error=\”Invalid JSON\”;¶” ;

JSONerror = Case (Left(JSONFormatElements(json) ; 1 ) = “?” ; 1 ; 0)

];

 

Case (

     //Error was found

     JSONerror = 1 ;

     Evaluate(“Let ( [ ” & error & raw  & “] ; \”\” )” ) ;

 

     //Create a local variable and call this function again to continue until no keys remain

     ValueCount(list)  > 0 ;

     Let ( [

          eval = Evaluate(“Let ( [ $” & key &  “=\”” & value & “\”] ; \”\” )” ) ;

          json = JSONDeleteElement ( json; key )

     ];  

          JPP (json)

     ) ;

     “”  //Finish when the loop is complete

) //end of Case statement

) //end of Let statement

Conclusion

What have you found JSON to be the most useful for?


Leave a Reply

two × four =