Working with JSON data in FileMaker

There are a number of ways of working with JSON data in FileMaker. At least two typical ones are:

  • Custom Functions based parsing & encoding
  • using a WebViewer & JavaScript

JSON however doesn’t parse easily with FileMaker’s text handling functions. And using a WebViewer has a number of complications, the main one being the awkwardness of getting data in & out of the WebViewer. Some other solutions only parse JSON, but don’t encode it. So I am going to propose yet another way.

This does have its own dependencies however. The bBox plugin and a small amount of Python scripting will be required. But the combination makes for a very concise implementation.

Basic JSON Encoding

Python has a number of good, robust, and fast JSON modules. For many purposes, the standard json module will do just fine.

Using this module, here is a complete FileMaker expression that queries the database and returns a JSON string:

 

A representative sample of the result yields this:

So what happened here?

  • set a variable to the contents of the Python script to run
  • created a Python VM and had the script compiled
  • executed the Python script
    • imported the needed Python modules
    • executed a SQL request in FileMaker, which was returned as a Python list
    • asked the json function to convert the Python list object into JSON text
    • Python returned the JSON string to FileMaker
  • Tore down the Python VM

It is normally easier to store the Python scripts in a field somewhere, since we must explicitly add the returns to the text when setting them via a FileMaker expression, but I’ve done it this to show you a full self-contained example.

Also, it is not a requirement that you call bBox_PythonFinalize. As a matter of fact, if you are going to re-run the same script again, you might be better off not calling it. Using either the fm.evaluate Python function or the FileMaker bBox_PythonSetVar function to pass the SQL expression might be one way to help re-use a previously compiled script.

JSON With Name & Value Pairs

Typically, JSON data comes in key/value pairs, so our first example is arguably not fully formed JSON yet. One way to add in key/value pairs is to append the keys to every value in the SQL result, and then convert that into a Python dictionary (dict) object. This sounds complicated and slow, but the performance hit is actually very slight, and by using a construct called a list comprehension, also easy to implement. While we are at, I’ll factor out the column names from the SQL.

To do this the FileMaker script is unchanged, so I’ll just show the FileMaker Set Variable script step used to set the Python script:

Running this new version against the demo database gives this result:

So far we’ve only discussed encoding into JSON data. In a next post, I hope to discuss some ways you can decode it.

Simon

Leave a Reply