{"id":11588,"date":"2021-10-21T17:21:10","date_gmt":"2021-10-21T17:21:10","guid":{"rendered":"https:\/\/www.directimpactsolutions.com\/?p=11588"},"modified":"2023-05-18T17:06:53","modified_gmt":"2023-05-18T17:06:53","slug":"automatically-parse-a-json-script-parameter","status":"publish","type":"post","link":"https:\/\/www.directimpactsolutions.com\/en\/automatically-parse-a-json-script-parameter\/","title":{"rendered":"Automatically Parse a JSON Script Parameter"},"content":{"rendered":"<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-1024x580.jpg\" alt=\"automatically parse a JSON script parameter\" class=\"wp-image-11783\" width=\"641\" height=\"363\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-1024x580.jpg 1024w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-300x170.jpg 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-768x435.jpg 768w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-600x340.jpg 600w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script.jpg 1500w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/figure><p>Beginning in FileMaker 16, many developers have used JSON functions to pass script parameters and script results, which can be a very convenient way to pass multiple parameters at once. However, it can be tedious to write many Set Variable script steps just to parse out values from the script parameter.<\/p><p>This blog will show how to use a custom function to automatically parse values in a JSON script parameter and set variables.<\/p><p>This is what a typical JSON script parameter looks like:<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"312\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter.jpg\" alt=\"\" class=\"wp-image-11589\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter.jpg 619w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter-300x151.jpg 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter-600x302.jpg 600w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/figure><p>In this example, we have 6 key-value pairs that we want to pass to a script as its script parameter. Traditionally, to parse out these values, we would use several&nbsp;<em>Set Variable<\/em>&nbsp;script steps, each using the JSONGetElement function to get the value of each key.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_2.jpg\" alt=\"\" class=\"wp-image-11591\" width=\"671\" height=\"237\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_2.jpg 842w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_2-300x106.jpg 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_2-768x271.jpg 768w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_2-600x212.jpg 600w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/figure><p>I use the same naming convention for my JSON keys and my variables, so when I parse JSON values to set variables, I typically name my variables the same as their corresponding keys.<\/p><h3 class=\"wp-block-heading\">Why not subscripts?<\/h3><p>If I always use my keys to name variables, can I automate this so I don\u2019t have to type everything? How do we automate setting variables?<\/p><p>Creating a sub-script is the first idea that comes to mind; we can easily loop through all keys in a JSON object using the Loop script step and then Set Variable to parse values, but given what we know about variable scopes, these variables are local to the sub-script, which means my parent script won\u2019t have access to it. So no, we can\u2019t use a sub-script to parse variables that we intend to use in our parent script.<\/p><p>Why don\u2019t we use global variables?<\/p><p>As a best practice, we should minimize the number of global variables, as it\u2019s easy to use them under the wrong context and cause issues. If you are not familiar with the variable scope, we\u2019ve created a&nbsp;<a href=\"https:\/\/youtu.be\/SVKZHxaj1Ec\" target=\"_blank\" rel=\"noreferrer noopener\">video<\/a>&nbsp;on this topic.<\/p><h3 class=\"wp-block-heading\">Using While (), Let (), and Evaluate () Functions<\/h3><p>What else can we use to set variables besides the Set Variable script step?<\/p><p>We can use the Let () and While () functions to set variables, including local variables. In this case, we need to use both. I need the While () function for looping and the Let () function for creating the variable. If you are not familiar with the While () function, we have a&nbsp;<a href=\"https:\/\/youtu.be\/7rueQpi0I0I\" target=\"_blank\" rel=\"noreferrer noopener\">video<\/a>&nbsp;on that, too.<\/p><p>So now we know how to loop and set variables using functions within the scope of the current script. How do we make the variable name dynamic?<\/p><p>The answer is the Evaluate () function, which will evaluate the expression given to it. We will dynamically construct Let () functions based on the data in the JSON script parameter, then use the Evaluate function on these expressions to create variables dynamically. As a warning, be careful when using the Evaluate function. It is a great way to add a layer of abstraction to your solution; make sure you know what you are doing.<\/p><p>Let\u2019s create the function. I\u2019ll create this in the Data Viewer first so that it\u2019s easier to test and debug.<\/p><p>We will start with a While () function. For the first parameter, we need to initialize some variables. We know we need the script parameter, so I will set a variable for that. Then I need to know what I\u2019m looping through. In this case, I\u2019m looping through keys in the JSON, so I should list all keys, using the JSONListKeys function.<\/p><p>We should count how many keys there are so we know when to stop. Since our looping condition is based on the number of keys, we should have a loop counter. Here I name my loop counter&nbsp;<em>i<\/em>&nbsp;out of habit.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_1.png\" alt=\"\" class=\"wp-image-11596\" width=\"456\" height=\"357\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_1.png 418w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_1-300x235.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure><p>Next, the loop condition. Under what circumstances should the loop continue to run? For us, as long as there are more keys to evaluate, the loop should run. So the condition should run if&nbsp;<em>i<\/em>&nbsp;is less than or equal to the total key count.<\/p><p>Additionally, we want to add another condition on the JSON input to make sure it is not empty.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_2.png\" alt=\"\" class=\"wp-image-11598\" width=\"455\" height=\"357\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_2.png 418w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_2-300x235.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/figure><p>After that comes the Logic parameter. We will start by getting the key for the current loop. Here I\u2019m double-checking that the key exists using a custom function from&nbsp;<a href=\"https:\/\/github.com\/geistinteractive\/fm-json-additions\" target=\"_blank\" rel=\"noreferrer noopener\">Todd Geist<\/a>. And of course, after getting the key, we want to get the value as well. So here we use JSONGetElement to parse out the value.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_3.png\" alt=\"\" class=\"wp-image-11600\" width=\"451\" height=\"317\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_3.png 567w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_3-300x211.png 300w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/figure><p>Now, this is where the magic happens! What do we want our Let () function to look like? It should be very simple where we set a local variable in the first parameter. Since we don\u2019t need it to return anything, the second parameter can simply be an empty string. If this is our goal, then the expression should look like this:<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_4.png\" alt=\"\" class=\"wp-image-11602\" width=\"448\" height=\"332\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_4.png 593w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_4-300x223.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/figure><p>After we have our expression, we can use the Evaluate function to actually create the variable. We could tweak this part so that, instead of constructing one Let function per loop, it constructs one large Let function that sets all variables at once, but we won\u2019t cover this technique in this post.<\/p><p>Of course, since the loop condition depends on the loop counter, we need to increase the loop counter by one, otherwise, we get stuck in an infinite loop. This is basically the only logic we need.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_5.png\" alt=\"\" class=\"wp-image-11604\" width=\"456\" height=\"289\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_5.png 654w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_5-300x190.png 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_5-600x380.png 600w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure><h3 class=\"wp-block-heading\" style=\"padding-top:var(--wp--preset--spacing--40)\">Results<\/h3><p>Now let\u2019s see our result. I created a demo file to show this in action. I have a \u201cScript Parameter\u201d field and two buttons.<\/p><p>The script attached to the \u201cTraditional Method\u201d button parses out each parameter using the JSONGetElement () function one by one, as shown below:<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_6.png\" alt=\"\" class=\"wp-image-11606\" width=\"551\" height=\"221\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_6.png 773w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_6-300x120.png 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_6-768x308.png 768w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_6-600x241.png 600w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/figure><p>Whereas the script attached to the \u201cCustom Function\u201d button uses the custom function we created earlier to parse out different parameters in one go:<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_7.png\" alt=\"\" class=\"wp-image-11608\" width=\"557\" height=\"121\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_7.png 572w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_7-300x65.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/figure><p>If I use the following data, both will parse out each parameter with identical results.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_8.png\" alt=\"\" class=\"wp-image-11610\" width=\"549\" height=\"199\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_8.png 651w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_8-300x109.png 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/While-_8-600x218.png 600w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><\/figure><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_4.png\" alt=\"\" class=\"wp-image-11612\" width=\"532\" height=\"181\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_4.png 829w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_4-300x102.png 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_4-768x261.png 768w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_4-600x204.png 600w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/figure><p>Both result in the same set of variables being created.<\/p><p>However, if I add one more key-value pair for gender, as pictured below:<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_5.png\" alt=\"\" class=\"wp-image-11614\" width=\"534\" height=\"196\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_5.png 655w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_5-300x110.png 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_5-600x221.png 600w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/figure><p>The traditional method will return the exact same result as the last time because it cannot parse out this additional parameter without being updated with another Set Variable script step.<\/p><p>But for the custom function method, since it\u2019s data-driven and dynamic, it will automatically parse out this additional parameter, as shown below.<\/p><figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_6.png\" alt=\"\" class=\"wp-image-11616\" width=\"521\" height=\"187\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_6.png 838w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_6-300x108.png 300w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_6-768x276.png 768w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/Script-Parameter_6-600x216.png 600w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/figure><h3 class=\"wp-block-heading\" style=\"padding-top:var(--wp--preset--spacing--40)\">Usage Considerations<\/h3><p>By combining While (), Let () and Evaluate () functions, we can dynamically parse JSON objects. I used it to parse script parameters, but you can certainly use it to parse other JSON results, too, perhaps request results from web services or APIs.<\/p><p>Keep in mind that by setting local variables using a Let () function, it\u2019s less obvious to other developers where these variables are set, so be mindful about the readability.<\/p><p>There may be situations where you only want to parse out specific values instead of all of them. In that case, the traditional method will give you more control.<\/p><p>We also demonstrate\u00a0<a href=\"https:\/\/youtu.be\/PmTfqxghIaE\" target=\"_blank\" rel=\"noreferrer noopener\">this topic<\/a>\u00a0on our YouTube channel.<\/p><p>If you have questions about how to automatically parse a JSON script parameter, 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>Beginning in FileMaker 16, many developers have used JSON functions to pass script parameters and script results, which can be a very convenient way to pass multiple parameters at once. However, it can be tedious to write many Set Variable script steps just to parse out values from the script parameter. This blog will show &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.directimpactsolutions.com\/en\/automatically-parse-a-json-script-parameter\/\"> <span class=\"screen-reader-text\">Automatically Parse a JSON Script Parameter<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":11,"featured_media":11783,"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,218],"class_list":["post-11588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-low-code","tag-filemaker","tag-json","tag-script-parameter"],"uagb_featured_image_src":{"full":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script.jpg",1500,850,false],"thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-150x150.jpg",150,150,true],"medium":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-300x170.jpg",300,170,true],"medium_large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-768x435.jpg",768,435,true],"large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-1024x580.jpg",1024,580,true],"1536x1536":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script.jpg",1500,850,false],"2048x2048":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script.jpg",1500,850,false],"woocommerce_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-300x300.jpg",300,300,true],"woocommerce_single":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-600x340.jpg",600,340,true],"woocommerce_gallery_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/05\/JSON-Script-100x100.jpg",100,100,true]},"uagb_author_info":{"display_name":"Weihao Ding","author_link":"https:\/\/www.directimpactsolutions.com\/en\/author\/weihao-dingdirectimpactsolutions-com\/"},"uagb_comment_info":0,"uagb_excerpt":"Beginning in FileMaker 16, many developers have used JSON functions to pass script parameters and script results, which can be a very convenient way to pass multiple parameters at once. However, it can be tedious to write many Set Variable script steps just to parse out values from the script parameter. This blog will show&hellip;","_links":{"self":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/11588","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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/comments?post=11588"}],"version-history":[{"count":5,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/11588\/revisions"}],"predecessor-version":[{"id":21013,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/11588\/revisions\/21013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media\/11783"}],"wp:attachment":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media?parent=11588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/categories?post=11588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/tags?post=11588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}