August 23, 2019 Blog FileMaker FileMaker Techniques FileMaker Training arrayFileMaker 18FileMaker Cloudfilemaker jsonJSONrecordsScripting Step-by-Step Guide to Building a JSON Object in FileMaker JSON has now become a FileMaker developer’s standard method to store and manipulate data in memory. In other words, when you want to create a variable to store anything more complex than a simple string, number, or return-separated list, JSON is the way to do it. If you’re not using it already, you must learn how! Starting out it can be a bit daunting, so I’m going to break it down for you. I already covered the basics of passing multiple parameters using JSON back when the feature was first introduced in version 16, but JSON can be used in FileMaker for much more than that. It can really shine when used to store structured data in a way that makes it easily accessible. I’m going to show you how to store FileMaker records as JSON and then extract the data you want using a script. You can test all of these steps using the Data Viewer in FileMaker’s advanced toolset, so follow along! Step 1 – Create a basic object The basic function we use to build JSON in FileMaker is the JSONSetElement function. The data viewer will autocomplete the following for you (just start typing “JSONSetElement”): JSONSetElement ( json ; keyOrIndexOrPath ; value ; type ) You’ll need to replace all the arguments with actual stuff, so: Change ‘json’ to just two double quotes: “” – this simply makes an empty JSON objectChange ‘keyOrIndexOrPath’ to firstName in quotes: “firstName” – this is called the ‘key’Change ‘value’ to your actual name in quotes: “David” – this is the value of the keyChange ‘type’ to “JSONString”. You can also just put another set of double quotes “”, and let FileMaker figure out what kind of data the value is. Congratulations, you just made a simple JSON object! It should evaluate to something like this: {"firstName":"David"} This just means that for this JSON object (all the stuff inside the curly braces), the key “firstName” is equal to the value “David”. In FileMaker terms, the ‘object’ is a record, the ‘key’ is a field name, and the ‘value’ is the contents of the field. Since this object represents a person, we should put it inside an object called “person”. We can adjust the code slightly like this: JSONSetElement ( "" ; "person.firstName" ; "David" ; JSONString ) Which results in a JSON object that looks like this: {"person":{"firstName":"David"}} Step 2 – Add fields Now we can start to construct a “record” in JSON. Take the JSON function you just wrote, and put some brackets around the 2nd, 3rd, and 4th arguments, like this: JSONSetElement ( “” ; [ “person.firstName” ; “David” ; JSONString ] ) Let’s add another field — this time the key will be “lastName”, and the value will be your last name. The way to add it as another “field” in this JSON object it to put a semicolon after the second bracket, and then replicate the structure of the first field, like so (I’ve added carriage returns and tabs to make the code more readable): JSONSetElement ( “” ; [ “person.firstName” ; “David” ; JSONString ] ; [ “person.lastName” ; “Weiner” ; JSONString ] ) You can continue to add as many keys (or fields) as you want: JSONSetElement ( “” ; [ “person.firstName” ; “David” ; JSONString ] ; [ “person.lastName” ; “Weiner” ; JSONString ] ; [ “person.age” ; 49 ; JSONNumber ] ; [ “person.phone” ; “(503)616-9422” ; JSONString ] ; [ “person.extension” ; 3 ; JSONNumber ] ; [ “person.hasPets” ; False ; JSONBoolean ] ) This chunk of code results in the following JSON: {"person":{"age":49,"extension":3,"firstName":"David","hasPets":false,"lastName":"Weiner","phone":"(503)616-9422"}} If we take the entire chunk of code and surround it with the JSONFormatElements function, we can make it look better. This doesn’t fundamentally change it at all, though. It just changes the way it displays: JSONFormatElements ( your JSONSetElement code here ) results in: { "person" : { "age" : 49, "extension" : 3, "firstName" : "David", "hasPets" : false, "lastName" : "Weiner", "phone" : "(503)616-9422" } } Notice that the keys inside the person object are alphabetized. This can look a little confusing, but that’s just the way it works. Also, notice that this is an object inside an object. The {curly braces} enclose a JSON object. This leads us to our next step… Step 3 – Add records Let’s say you want to add another person record to this JSON. The usual way to start collecting an array of records in JSON would be to hold it in a variable, so let’s assume you started with a variable, called $json, and it contained the data from the previous step. We actually need to put an array (which is simply a list) of records inside the person object. The array is a list of JSON objects, which each contain a set of fields. We need to adjust our last bit of code to create an array: JSONSetElement ( “” ; [ “person[0].firstName” ; “David” ; JSONString ] ; [ “person[0].lastName” ; “Weiner” ; JSONString ] ; [ “person[0].age” ; 49 ; JSONNumber ] ; [ “person[0].phone” ; “(503)616-9422” ; JSONString ] ; [ “person[0].extension” ; 3 ; JSONNumber ] ; [ “person[0].hasPets” ; False ; JSONBoolean ] ) This just tells the function to put all the information inside the first slot of an array inside the person object. JSON is 0 indexed, so the first slot is index [0], then [1], [2], etc. The result is this (note the [square braces], which indicate the array): { "person" : [ { "age" : 49, "extension" : 3, "firstName" : "David", "hasPets" : false, "lastName" : "Weiner", "phone" : "(503)616-9422" } ] } Now, you can inject a new item into the $json variable, inside the person array by referencing the next index number: JSONSetElement ( $json ; [ “person[1].firstName” ; “Kimberly” ; JSONString ] ; [ “person[1].lastName” ; “Carlson” ; JSONString ] ; [ “person[1].phone” ; “(503)616-9422” ; JSONString ] ; [ “person[1].extension” ; 2 ; JSONNumber ] ; [ “person[1].hasPets” ; True ; JSONBoolean ] ) Which results in the following: { "person" : [ { "age" : 49, "extension" : 3, "firstName" : "David", "hasPets" : false, "lastName" : "Weiner", "phone" : "(503)616-9422" }, { "extension" : 2, "firstName" : "Kimberly", "hasPets" : true, "lastName" : "Carlson", "phone" : "(503)616-9422" } ] } Now we have two records in the JSON array! That’s basically it. You can continue to add as many records as you want. You can even make arrays inside of arrays. Then you can pull out individual items with the JSONGetElement function like this: JSONGetElement ( $json ; "person[1].firstName" ) I use this technique regularly to loop through records, pulling out bits of data from each one and storing them in an array. Then I can move to another context and parse items from the array and deposit them elsewhere, or even export them! There are so many ways you can use this. What do you think you could do with it? By David Weiner
12 Comments Jonathan Fletcher Posted on 12:27 PM - August 28, 2019 This is one of the best JSON intros I have seen, David. Thanks! Arild Schönberg Posted on 2:35 PM - September 29, 2019 Agreed! I’ve used JSON and have found it cumbersome. This explanation sheds light and clarity when referring to the content as records, fields and data. Thank you! David Weiner Posted on 11:28 AM - September 30, 2019 So glad is was helpful! Charles Posted on 3:07 PM - June 13, 2020 JSONSetElement ( $json ; [ “person[1].firstName” ; “Kimberly” ; JSONString ] ; [ “person[1].lastName” ; “Carlson” ; JSONString ] ) is the second record, cool yet in my looping I try “person[$counter].firstName”; tableName::fName; JSONString” and it fails. How do you build the JSON from a loop? David Weiner Posted on 9:41 AM - June 15, 2020 Try this: “person[” & $counter & “].firstName” ; tableName::fName ; JSONString Ninad Dixit Posted on 4:32 AM - June 24, 2020 Useful blog, David. The procedure to create a JSON object in FileMaker is explained clearly. The concept is well explained with the code. The information provided will be very helpful for developers new to FileMaker. They can learn many concepts from this blog. JSON is very useful while using FIleMaker. Eduardo Ulloa Posted on 6:39 PM - August 13, 2020 Hola, una vez creado el objeto json, este queda en la tabla, en un campo calculado. como hago para publicar un conjunto de registros en una API? David Weiner Posted on 11:12 AM - August 14, 2020 Para publicar los registros con una API, necesita utilizar el API de los datos de FileMaker: https://fmhelp.filemaker.com/docs/18/es/dataapi/ Éric Savard Posted on 10:12 AM - February 1, 2021 Hello a try to do tha in FM 18 wiht the JSONSetElement and i cant reproduce ? { “Line”: [ { “DetailType”: “SalesItemLineDetail”, “Amount”: 100.0, “SalesItemLineDetail”: { “ItemRef”: { “name”: “Services”, “value”: “1” } } } ], “CustomerRef”: { “value”: “1” } } David Weiner Posted on 3:51 PM - February 1, 2021 Try this: JSONSetElement ( “” ; [ “Line[0].DetailType” ; “SalesItemLineDetail” ; JSONString ]; [ “Line[0].Amount” ; “100.0” ; JSONstring ]; [ “Line[0].SalesItemLineDetail.ItemRef.name” ; “Services” ; JSONString ]; [ “Line[0].SalesItemLineDetail.ItemRef.value” ; 1 ; JSONstring ]; [ “CustomerRef.value” ; 1 ; JSONstring ] ) Éric Savard Posted on 11:58 AM - May 17, 2021 Hello so i do that JSONFormatElements ( JSONSetElement ( “{}” ; [ “DocNumber” ; Facturation::NoFacture; JSONString ] ; [ “CustomerRef” ; JSONSetElement (“{}” ; [ “value” ; 67; JSONString ] ) ;JSONObject ]; [ “Line[0].DetailType” ; “SalesItemLineDetail” ; JSONString ]; [ “Line[0].Description” ; Facture_Items::Description; JSONString ]; [ “Line[0].Amount” ; Facture_Items::Total_Cal ; JSONString ]; [ “Line[0].SalesItemLineDetail.ItemRef.value” ; Facture_Items::Produit ;JSONString ]; [ “Line[0].Qty” ; Facture_Items::Qts_Expedier ; JSONString ]; [ “Line[0].SalesItemLineDetail.ItemAccountRef.value” ; Facture_Items::Produit ; JSONString ]; [ “Line[0].SalesItemLineDetail.TaxCodeRef.value” ; 5 ; JSONString ] ) ) and it’s look like this: { “CustomerRef” : { “value” : “67” }, “DocNumber” : “21316”, “Line” : [ { “Amount” : “272.75”, “Description” : “Étiquette non conforme”, “DetailType” : “SalesItemLineDetail”, “Qty” : “1100”, “SalesItemLineDetail” : { “ItemAccountRef” : { “value” : “6” }, “ItemRef” : { “value” : “6” }, “TaxCodeRef” : { “value” : “5” } } } ] } but i try to put here this. { “CustomerRef” : { “value” : “67” }, “DocNumber” : “21316”, “Line” : [ { “Amount” : “272.75”, “Description” : “Étiquette non conforme”, “DetailType” : “SalesItemLineDetail”, “SalesItemLineDetail” : { “ItemAccountRef” : { “value” : “6” }, “ItemRef” : { “value” : “6” }, “Qty” : “1100”, “TaxCodeRef” : { “value” : “5” } } } ] } and it dosent work David Weiner Posted on 1:00 PM - May 17, 2021 Hello, Éric – I’m not entirely sure what the root of the issue is, but I’m confident that we could help solve your problem if we spent a little more time on it. You can book a meeting with our sales team by filling out the form on this page: https://app.works/contact-us/