Exporting JSON Data

The next step in creating a dynamically generated organization chart is to transfer the generated JSON from the FileMaker database to the OmniGraffle document. This is accomplished by creating and running another FileMaker script that encodes and inserts the JSON into an Omni Automation script that writes the data into the OmniGraffle document.

Here is the script:

FileMaker script for exporting JSON

 1  Perform the FileMaker script for generating JSON for the iterated set of database records. If you remember from the previous page, the resulting JSON is returned by the script when it has completed.

 2  Instantiate a new variable named $encodedOrgData that uses the FileMaker GetAsURLEncoded function to percent encode the JSON and store the result in the created variable.

 3  Instantiate a new variable named $dbName that uses the FileMaker GetAsURLEncoded function to percent encode the database file name and store the result in the created variable.

 4  Instantiate a new variable named $omniscript that contains a percent encoded version of the Omni Automation script shown later on this page.

 4  Execute the Omni Automation script by opening the script URL.

As shown by the illustration below, in the FileMaker script statement that creates the $omniscript variable that contains the encoded Omni Automation script (FileMaker script line  4   above), the placeholders DBTRANSFERDATA (below: script line 1) and DBFILENAME (below: script line 2) are replaced with the created FileMaker variables $encodedOrgData and $dbName respectively.

Create the record JSON

When the encoded Omni Automation script URL is executed, it will include the JSON data extracted from the database.

The Tagging Script

The executed Omni Automation script performs two tasks with the current OmniGraffle document:

The following is a line-by-line explanation of the Omni Automation script:

 1  In the encoded version of this script, the placeholder DBTRANSFERDATA is replaced with the percent encoded JSON database data. When the script URL is processed by OmniGraffle, it is decoded and the script variable JSONstr will be assigned the transfered JSON.

 2  The script variable databaseFileName wil be assigned the name of the source FileMaker database.

 3  A conditional statement that checks for a single selected solid to be assigned the values of the first database record. A solid is a graphic that can contain text or an image. If a single solid is not selected, then alert the script user.

 9-28  The script statements for tagging document objects with the transferred data.

JSONstr = 'DBTRANSFERDATA' databaseFileName = 'DBFILENAME' if (document.windows[0].selection.solids.length != 1){ title = "SELECTION ERROR" message = "Please select a single solid graphic." new Alert(title, message).show(function(result){}) throw new Error(title) } else { cnvs = document.windows[0].selection.canvas graphic = document.windows[0].selection.solids[0] // retrieve data for top-level item data = JSON.parse(JSONstr) topLevelName = data[0]['Name'] topLevelID = data[0]['Employee ID'] // write name and ID to graphic graphic.name = topLevelName + ' ' + topLevelID // write name and ID to graphic text graphic.text = topLevelName + '\n' + topLevelID // write metadata to graphic user data graphic.setUserData('Employee Name',topLevelName) graphic.setUserData('Employee ID',topLevelID) graphic.setUserData('Database Name',databaseFileName) // write JSON string to canvas notes field cnvs.background.notes = JSONstr // confirmation alert title = "DATA TRANSFERED" message = "The organization data has been successfully transfered." new Alert(title, message).show(function(result){}) }
omnigraffle://localhost/omnijs-run?script=JSONstr%20%3D%20%27DBTRANSFERDATA%27%0Aif%20%28document%2Ewindows%5B0%5D%2Eselection%2Esolids%2Elength%20%21%3D%201%29%7B%0A%09title%20%3D%20%22SELECTION%20ERROR%22%0A%09message%20%3D%20%22Please%20select%20a%20single%20graphic%2E%22%0A%09new%20Alert%28title%2C%20message%29%2Eshow%28function%28result%29%7B%7D%29%0A%09throw%20new%20Error%28title%29%0A%7D%20else%20%7B%0A%09cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0A%09graphic%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Esolids%5B0%5D%0A%09%2F%2F%20retrieve%20data%20for%20top-level%20item%0A%09data%20%3D%20JSON%2Eparse%28JSONstr%29%0A%09topLevelItem%20%3D%20data%5B1%5D%0A%09topLevelName%20%3D%20data%5B0%5D%5B%27Name%27%5D%0A%09topLevelID%20%3D%20data%5B0%5D%5B%27Employee%20ID%27%5D%0A%09%2F%2F%20write%20name%20and%20ID%20to%20graphic%0A%09graphic%2Ename%20%3D%20topLevelName%20%2B%20%27%20%27%20%2B%20topLevelID%0A%09%2F%2F%20write%20name%20and%20ID%20to%20graphic%20text%0A%09graphic%2Etext%20%3D%20topLevelName%20%2B%20%27%5Cn%27%20%2B%20topLevelID%0A%09%2F%2F%20write%20name%20and%20ID%20to%20graphic%20user%20data%0A%09graphic%2EsetUserData%28%27Employee%20Name%27%2CtopLevelName%29%0A%09graphic%2EsetUserData%28%27Employee%20ID%27%2CtopLevelID%29%0A%09%2F%2F%20write%20JSON%20string%20to%20canvas%20notes%20field%0A%09cnvs%2Ebackground%2Enotes%20%3D%20JSONstr%0A%09%2F%2F%20confirmation%20alert%0A%09title%20%3D%20%22DATA%20TRANSFERED%22%0A%09message%20%3D%20%22The%20organization%20data%20has%20been%20successfully%20transfered%2E%22%0A%09new%20Alert%28title%2C%20message%29%2Eshow%28function%28result%29%7B%7D%29%0A%7D

 9,10  Instantiate variables for the current canvas and the selected graphic (solid).

 12  Use the parse() method to convert the JSON string into a JSON object.

 13-14  Extract the Employee Name and Employee ID for the top-level database record (record 1).

 16  Name the graphic using the top-level Employee Name and Employee ID data.

 18  Set the text in the graphic to the top-level Employee Name, a carriage return, and the Employee ID data.

 20-22  Assign metadata keys and values to the selected graphic.

 24  Set the value of the Notes field of the current canvas to the full JSON transferred from the database.

(below) The top-level record object name and metadata assigned to the selected graphic:

tagged-top-level-item

(below) The JSON generated from the FileMaker database is placed into the Notes field for the current canvas.

tagged-top-level-item

Next

In the next section, we’ll examine how to dynamically generate an organization chart using the actions in an Omni Automation PlugIn.

UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER