{"id":11473,"date":"2019-08-23T18:21:23","date_gmt":"2019-08-23T18:21:23","guid":{"rendered":"https:\/\/www.directimpactsolutions.com\/?p=11473"},"modified":"2023-05-18T17:13:19","modified_gmt":"2023-05-18T17:13:19","slug":"guide-to-building-a-json-object","status":"publish","type":"post","link":"https:\/\/www.directimpactsolutions.com\/en\/guide-to-building-a-json-object\/","title":{"rendered":"Easy Step-by-Step Guide to Building a JSON Object in FileMaker"},"content":{"rendered":"<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg\" alt=\"step-by-step guide to creating a JSON object in FileMaker\" class=\"wp-image-11752\" width=\"607\" height=\"404\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg 680w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-300x200.jpeg 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-600x400.jpeg 600w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/figure><p>JSON has now become a FileMaker developer\u2019s 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\u2019re not using it already, you must learn how!<\/p><p>Starting out it can be a bit daunting, so I\u2019m going to break it down for you. It can really shine when used to store structured data in a way that makes it easily accessible. I\u2019m going to show you how to store FileMaker records as JSON and then extract the data you want using a script.<\/p><p>You can test all of these steps using the&nbsp;<a href=\"https:\/\/fmhelp.filemaker.com\/help\/18\/fmp\/en\/#page\/FMP_Help%2Fdata-viewer.html\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Data Viewer<\/em><\/a>&nbsp;in FileMaker\u2019s advanced toolset, so follow along!<\/p><h3 class=\"wp-block-heading\">Step 1 \u2013 Create a basic object<\/h3><p>The basic function we use to build JSON in FileMaker is the&nbsp;<em><a href=\"https:\/\/fmhelp.filemaker.com\/help\/18\/fmp\/en\/#page\/FMP_Help%2Fjsonsetelement.html\" target=\"_blank\" rel=\"noreferrer noopener\">JSONSetElement function<\/a><\/em>. The data viewer will autocomplete the following for you (just start typing \u201cJSONSetElement\u201d):<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( json ; keyOrIndexOrPath ; value ; type )<\/code><\/pre><p>You\u2019ll need to replace all the arguments with actual stuff, so:<\/p><ul class=\"wp-block-list\"><li>Change \u2018json\u2019 to just two double quotes: \u201c\u201d \u2013 this simply makes an empty JSON object<\/li>\n\n<li>Change \u2018keyOrIndexOrPath\u2019 to firstName in quotes: \u201cfirstName\u201d \u2013 this is called the \u2018key\u2019<\/li>\n\n<li>Change \u2018value\u2019 to your actual name in quotes: \u201cDavid\u201d \u2013 this is the value of the key<\/li>\n\n<li>Change \u2018type\u2019 to \u201cJSONString\u201d. You can also just put another set of double quotes \u201c\u201d, and let FileMaker figure out what kind of data the value is.<\/li><\/ul><p>Congratulations, you just made a simple JSON object! It should evaluate to something like this:<\/p><pre class=\"wp-block-code\"><code>{\"firstName\":\"David\"}<\/code><\/pre><p>This just means that for this JSON object (all the stuff inside the curly braces), the key \u201cfirstName\u201d is equal to the value \u201cDavid\u201d. In FileMaker terms, the \u2018object\u2019 is a record, the \u2018key\u2019 is a field name, and the \u2018value\u2019 is the contents of the field.&nbsp;<\/p><p>Since this object represents a person, we should put it inside an object called \u201cperson\u201d. We can adjust the code slightly like this:<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( \"\" <strong>;<\/strong> \"<strong>person.<\/strong>firstName\" <strong>;<\/strong> \"David\" <strong>;<\/strong> JSONString )<\/code><\/pre><p>Which results in a JSON object that looks like this:<\/p><pre class=\"wp-block-code\"><code>{\"person\":{\"firstName\":\"David\"}}<\/code><\/pre><h3 class=\"wp-block-heading\">Step 2 \u2013 Add fields<\/h3><p>Now we can start to construct a \u201crecord\u201d in JSON. Take the JSON function you just wrote, and put some brackets around the 2nd, 3rd, and 4th arguments, like this:<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( \u201c\u201d ; <strong>&#91; <\/strong>\u201cperson.firstName\u201d ; \u201cDavid\u201d ; JSONString <strong>]<\/strong> )<\/code><\/pre><p>Let\u2019s add another field \u2014 this time the key will be \u201clastName\u201d, and the value will be your last name. The way to add it as another \u201cfield\u201d 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\u2019ve added carriage returns and tabs to make the code more readable):<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( \u201c\u201d <strong>;<\/strong> \n     &#91; \u201cperson.firstName\u201d <strong>;<\/strong> \u201cDavid\u201d <strong>; <\/strong>JSONString ] <strong>;<\/strong> \n  <strong>   &#91; \u201cperson.lastName\u201d ; \u201cWeiner\u201d ; JSONString ] <\/strong>\n     )<\/code><\/pre><p>You can continue to add as many keys (or fields) as you want:<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( \u201c\u201d <strong>;<\/strong> \n     &#91; \u201cperson.firstName\u201d <strong>;<\/strong> \u201cDavid\u201d <strong>;<\/strong> JSONString ] <strong>;<\/strong> \n     &#91; \u201cperson.lastName\u201d <strong>;<\/strong> \u201cWeiner\u201d <strong>;<\/strong> JSONString ] <strong>;<\/strong> \n     <strong>&#91; \u201cperson.age\u201d ; 49 ; JSONNumber ]  ; \n     &#91; \u201cperson.phone\u201d ; \u201c(503)616-9422\u201d ; JSONString ] ; \n     &#91; \u201cperson.extension\u201d ; 3 ; JSONNumber ]  ; \n     &#91; \u201cperson.hasPets\u201d ; False ; JSONBoolean ] <\/strong> \n     )<\/code><\/pre><p>This chunk of code results in the following JSON:<\/p><pre class=\"wp-block-code\"><code>{\"person\":{\"age\":49,\"extension\":3,\"firstName\":\"David\",\"hasPets\":false,\"lastName\":\"Weiner\",\"phone\":\"(503)616-9422\"}}<\/code><\/pre><p>If we take the entire chunk of code and surround it with the&nbsp;<a href=\"https:\/\/fmhelp.filemaker.com\/help\/18\/fmp\/en\/#page\/FMP_Help%2Fjsonformatelements.html\" target=\"_blank\" rel=\"noreferrer noopener\"><em>JSONFormatElements<\/em><\/a>&nbsp;function, we can make it look better. This doesn\u2019t fundamentally change it at all, though. It just changes the way it displays:<\/p><p class=\"has-vivid-red-color has-text-color has-background\" style=\"background-color:#f78da842;padding-top:var(--wp--preset--spacing--20);padding-right:var(--wp--preset--spacing--20);padding-bottom:var(--wp--preset--spacing--20);padding-left:var(--wp--preset--spacing--20);font-size:15px\">JSONFormatElements (&nbsp;<em>your JSONSetElement code here<\/em>&nbsp;)&nbsp;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\">results in:<\/mark><\/p><pre class=\"wp-block-code\"><code>{\n      \"person\" : \n      {\n           \"age\" : 49,\n           \"extension\" : 3,\n           \"firstName\" : \"David\",\n           \"hasPets\" : false,\n           \"lastName\" : \"Weiner\",\n           \"phone\" : \"(503)616-9422\"\n      }\n}<\/code><\/pre><p>Notice that the keys inside the person object are alphabetized. This can look a little confusing, but that\u2019s 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\u2026<\/p><h3 class=\"wp-block-heading\">Step 3 \u2013 Add records<\/h3><p>Let\u2019s 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\u2019s 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:<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( \u201c\u201d ; \n     &#91; \u201cperson<strong>&#91;0].<\/strong>firstName\u201d ; \u201cDavid\u201d ; JSONString ] ; \n     &#91; \u201cperson<strong>&#91;0].<\/strong>lastName\u201d ; \u201cWeiner\u201d ; JSONString ]  ; \n     &#91; \u201cperson<strong>&#91;0].<\/strong>age\u201d ; 49 ; JSONNumber ]  ; \n     &#91; \u201cperson<strong>&#91;0].<\/strong>phone\u201d ; \u201c(503)616-9422\u201d ; JSONString ] ; \n     &#91; \u201cperson<strong>&#91;0].<\/strong>extension\u201d ; 3 ; JSONNumber ]  ; \n     &#91; \u201cperson<strong>&#91;0].<\/strong>hasPets\u201d ; False ; JSONBoolean ]  \n     )<\/code><\/pre><p>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):<\/p><pre class=\"wp-block-code\"><code>{\n      \"person\" : \n      &#91;\n            {\n                   \"age\" : 49,\n                   \"extension\" : 3,\n                   \"firstName\" : \"David\",\n                   \"hasPets\" : false,\n                   \"lastName\" : \"Weiner\",\n                   \"phone\" : \"(503)616-9422\"\n            }\n       ]\n}<\/code><\/pre><p>Now, you can inject a new item into the $json variable, inside the person array by referencing the next index number:<\/p><pre class=\"wp-block-code\"><code>JSONSetElement ( $json ; \n     &#91; \u201cperson<strong>&#91;1].<\/strong>firstName\u201d ; \u201c<strong>Kimberly<\/strong>\u201d ; JSONString ] ; \n     &#91; \u201cperson<strong>&#91;1].<\/strong>lastName\u201d ; \u201c<strong>Carlson<\/strong>\u201d ; JSONString ]  ; \n     &#91; \u201cperson<strong>&#91;1].<\/strong>phone\u201d ; \u201c<strong>(503)616-9422<\/strong>\u201d ; JSONString ] ; \n     &#91; \u201cperson<strong>&#91;1].<\/strong>extension\u201d ; <strong>2<\/strong> ; JSONNumber ]  ; \n     &#91; \u201cperson<strong>&#91;1].<\/strong>hasPets\u201d ; <strong>True<\/strong> ; JSONBoolean ]  \n     )<\/code><\/pre><p>Which results in the following:<\/p><pre class=\"wp-block-code\"><code>{\n      \"person\" : \n      &#91;\n            {\n                   \"age\" : 49,\n                   \"extension\" : 3,\n                   \"firstName\" : \"David\",\n                   \"hasPets\" : false,\n                   \"lastName\" : \"Weiner\",\n                   \"phone\" : \"(503)616-9422\"\n             },\n             {\n                   \"extension\" : 2,\n                   \"firstName\" : \"Kimberly\",\n                   \"hasPets\" : true,\n                   \"lastName\" : \"Carlson\",\n                   \"phone\" : \"(503)616-9422\"\n             }\n       ]\n}<\/code><\/pre><p>Now we have two records in the JSON array! That\u2019s 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&nbsp;<a href=\"https:\/\/fmhelp.filemaker.com\/help\/18\/fmp\/en\/#page\/FMP_Help%2Fjsongetelement.html\" target=\"_blank\" rel=\"noreferrer noopener\"><em>JSONGetElement function<\/em><\/a>&nbsp;like this:<\/p><pre class=\"wp-block-code\"><code>JSONGetElement ( $json ; \"person&#91;1].firstName\" ) <\/code><\/pre><p>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?<\/p><p>If you have question regarding this method, feel free to <a href=\"http:\/\/directimpactsolutions.com\/en\/contact-us\/\">contact us<\/a>!<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\" style=\"margin-top:var(--wp--preset--spacing--70);margin-bottom:var(--wp--preset--spacing--70)\"\/><p class=\"has-small-font-size\"><em>*This article was originally written for AppWorks, which has since joined Direct Impact Solutions. This article is intended for informative purposes only. To the best of our knowledge, this information is accurate as of the date of publication.<\/em><\/p>","protected":false},"excerpt":{"rendered":"<p>JSON has now become a FileMaker developer\u2019s 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\u2019re not using it already, you must learn &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.directimpactsolutions.com\/en\/guide-to-building-a-json-object\/\"> <span class=\"screen-reader-text\">Easy Step-by-Step Guide to Building a JSON Object in FileMaker<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":23,"featured_media":11752,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"","footnotes":""},"categories":[29],"tags":[39,201,202],"class_list":["post-11473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-low-code","tag-filemaker","tag-json","tag-json-object"],"uagb_featured_image_src":{"full":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg",680,453,false],"thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-150x150.jpeg",150,150,true],"medium":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-300x200.jpeg",300,200,true],"medium_large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg",680,453,false],"large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg",680,453,false],"1536x1536":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg",680,453,false],"2048x2048":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON.jpeg",680,453,false],"woocommerce_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-300x300.jpeg",300,300,true],"woocommerce_single":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-600x400.jpeg",600,400,true],"woocommerce_gallery_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2019\/08\/JSON-100x100.jpeg",100,100,true]},"uagb_author_info":{"display_name":"David Weiner","author_link":"https:\/\/www.directimpactsolutions.com\/en\/author\/david-weiner\/"},"uagb_comment_info":0,"uagb_excerpt":"JSON has now become a FileMaker developer\u2019s 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\u2019re not using it already, you must learn&hellip;","_links":{"self":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/11473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/comments?post=11473"}],"version-history":[{"count":6,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/11473\/revisions"}],"predecessor-version":[{"id":12279,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/11473\/revisions\/12279"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media\/11752"}],"wp:attachment":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media?parent=11473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/categories?post=11473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/tags?post=11473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}