Thursday, June 20, 2019

Communicating with Apple Web Services (GSX) via Filemaker 2

My previous blog on the same subject is 3 years ago. Since then Apple has changed from using XML to JSON as a means of transferring data. Therefore there is a need to show how to do it with JSON.

JSON (Javascript Object Notion) by its name, is a Javascript feature but it has extended its use to many other applications. It is a format of storing data just like XML except it is more human readable when you format it nicely.

Fortunately, FileMaker has caught up with the latest trend by including a few JSON related functions. It is not that easy to use those functions to compose JSON but it is very much easy to get data from JSON.

To create simple JSON you just need to use $example =JSONSetElement("";"elementkey";"elementvalue";1) and it will create a nice JSON '{"elementkey":"elementvalue"}'. However, if you want to insert nested JSON then it is a bit tedious.

For example, you want to create a key that contains an array with one key and  value. First you must create the key and value as a JSON like last paragraph. Then you have to create an array to contain that JSON like $array= JSONSetElement("[]";0;$example;3).

Finally you then create the root element like $root=JSONSetElement("";"root";$array;4). You have created a JSON '{"root":[{"elementkey":"elementvalue"}]}'. This format is generally used by Apple in GSX in both request and response.

If it is a simple JSON that need to store a small set of data with fixed keys, it is better to create the JSON with unique values for each element that needs to be changed like "~!1", "~!2" etc. After that use substitute function to replace the "~!x" sequence. Two lines instead of 3. In actual fact, one line encompassing both action can be done by just using the JSON as source of substitute directly. In this way you could reuse the same code repeatedly or even store it as custom function.

The parameters used in JSONSetElement can be found at. JSONSetElement.

Getting values from "elementkey" in the above JSON is easy. $value=JSONGetElement($root;"root[0].elementkey")

If you looking at this blog first then I suggest you go to the previous blog on the same subject first to get an understanding.


No comments:

Post a Comment