{"componentChunkName":"component---src-templates-default-template-tsx","path":"/ki-syntax/definitions/varstrings/","result":{"data":{"asciidoc":{"id":"feba8521-e546-5153-8f0c-defe2efe073e","html":"<div id=\"toc\" class=\"toc\">\n<div id=\"toctitle\">Table of Contents</div>\n<ul class=\"sectlevel1\">\n<li><a href=\"#_mathematical_operations\">Mathematical operations</a></li>\n<li><a href=\"#_string_modifiers\">String Modifiers</a></li>\n<li><a href=\"#varstring-json\">JSON handling with VarStrings</a></li>\n</ul>\n</div>\n<div id=\"preamble\">\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>At almost every location in a Knowledge Item where strings could be\nentered, the engine also processes \"VarStrings\". VarStrings are strings\nthat can be dynamically expanded during the runtime with specific\nvariable values. This works as follows:</p>\n</div>\n<div class=\"paragraph\">\n<p>It is possible to construct strings using string interpolation:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">on\n  Foo\n  Bar == \"Hello, ${Foo}\"</code></pre>\n</div>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_mathematical_operations\">Mathematical operations</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>It is possible to use math expressions on the right side of a match:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">on\n  Foo\n  Bar == Foo + 1</code></pre>\n</div>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">log(\"This is the ID of the current node: ${NodeID}\")</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>In addition to this simple string replacement, we can perform some mathematical operations:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Addition\nLOCAL::result = number1 + number2\nlog(\"${number1} + ${number2} = ${LOCAL::result}\")</code></pre>\n</div>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Subtraction\nLOCAL::result = number1 - number2\nlog(\"${number1} - ${number2} = ${LOCAL::result}\")</code></pre>\n</div>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Division\nLOCAL::result = number1 / number2\nlog(\"${number1} / ${number2} = ${LOCAL::result}\")</code></pre>\n</div>\n</div>\n<div class=\"admonitionblock important\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-important\" title=\"Important\"></i>\n</td>\n<td class=\"content\">\n<div class=\"paragraph\">\n<p>When the result of a division is about to be assigned to a variable, if division by 0 was attempted, the value of the variable will be set to 'MathError'. It&#8217;s important to note that using 'MathError' in subsequent operations can lead to further arithmetic errors in the following KIs</p>\n</div>\n</td>\n</tr>\n</table>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Multiplication\nLOCAL::result = number1 * number2\nlog(\"${number1} * ${number2} = ${LOCAL::result}\")</code></pre>\n</div>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Mod\nLOCAL::result = number1 % number2\nlog(\"${number1} % ${number2} = ${LOCAL::result}\")</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>There is also a mathematical library integrated into the engine which allows the\nevaluation of mathematical formulas. For this purpose, the corresponding\nexpression is enclosed in $[ ]:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Multiplication\nlog(\"Calculated Value: $[${number1} * ${number2}]\")</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>The engine also integrates a mathematical library (<a href=\"https://github.com/Rob-bie/expr\">https://github.com/Rob-bie/expr</a>) that enables the evaluation of mathematical formulas. To use this funcionality, the relevant expression must be enclosed in <code>$[ ]</code>.\nWhen including a variable in the mathematical formula, the variable should be enclosed in <code>${}</code></p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\"># Absolute value\nValue = \"$[abs(-20)]\"\n\n# Absolute value using variables\nTS1 = \"1732542005189\"\nTS2 = \"1732539624000\"\n\nTS3 = \"$[abs(${TS1} - ${TS2})]\"</code></pre>\n</div>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_string_modifiers\">String Modifiers</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>Also, various modifiers can be used in variable strings to convert the\noutput. Let&#8217;s assume we have a variable \"data\" with a string as value. We can modify the value \"hello world\" in the following ways:</p>\n</div>\n<div class=\"admonitionblock note\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-note\" title=\"Note\"></i>\n</td>\n<td class=\"content\">\nModifiers must be put before scope declaration, not before variable: <code>${+LOCAL::test}</code>\n</td>\n</tr>\n</table>\n</div>\n<table class=\"tableblock frame-all grid-all stretch\">\n<caption class=\"title\">Table 1. Modifiers</caption>\n<colgroup>\n<col style=\"width: 33.3333%;\">\n<col style=\"width: 33.3333%;\">\n<col style=\"width: 33.3334%;\">\n</colgroup>\n<thead>\n<tr>\n<th class=\"tableblock halign-left valign-top\"></th>\n<th class=\"tableblock halign-left valign-top\">Description</th>\n<th class=\"tableblock halign-left valign-top\">Sample from \"Hello world\"</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">+</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Convert to uppercase</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">\"HELLO WORLD\"</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">-</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Convert to lowercase</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">\"hello world\"</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">^</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Convert to upper camelcase</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">\"Hello World\"</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">\\</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Shell encoding (extra backslash for \" and \\ )</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">\\\"Hello world\\\"</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">&amp;</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">HTML encoding</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">&amp;quot;Hello world&amp;quot;</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">_</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Convert non alphanumerical chars to underscore</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\"><em>Hello_world</em></p></td>\n</tr>\n</tbody>\n</table>\n<div class=\"listingblock\">\n<div class=\"title\">Syntax</div>\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">upper =  \"${+data}\"\nlower =  \"${-data}\"\ncamel =  \"${^data}\"\nshell = \"${\\data}\"\nhtml = \"${&amp;data}\"\nunder =  \"${_data}\"</code></pre>\n</div>\n</div>\n<div class=\"admonitionblock tip\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-tip\" title=\"Tip\"></i>\n</td>\n<td class=\"content\">\n<div class=\"paragraph\">\n<p>These can also be combined as required, so if the value is\n\"hello world\" as above, you can combine uppercase and underscore conversion ${+_HELLOWORLD} for a result like: <em>HELLO_WORLD</em>.</p>\n</div>\n</td>\n</tr>\n</table>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"varstring-json\">JSON handling with VarStrings</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>VarStrings allow simple parsing of JSON structures.</p>\n</div>\n<div class=\"paragraph\">\n<p>Handling of JSON structures is initialized by adding <code>#</code> to a variable name inside the VarString followed by operators to address <code>Key:Value</code> pairs inside the structure.</p>\n</div>\n<table class=\"tableblock frame-all grid-all stretch\">\n<caption class=\"title\">Table 2. JSON Operators</caption>\n<colgroup>\n<col style=\"width: 50%;\">\n<col style=\"width: 50%;\">\n</colgroup>\n<thead>\n<tr>\n<th class=\"tableblock halign-left valign-top\">Operator</th>\n<th class=\"tableblock halign-left valign-top\">Function</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\"><code>key</code></p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Name of \"key\" in the root JSON object</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\"><code>['subkey']</code></p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Name of \"subkey\" in a JSON sub-object</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\"><code>[0&#8230;&#8203;n]</code></p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">n-th element of a JSON (sub-)array item (first element is 0).</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\"><code>[-n&#8230;&#8203;-1]</code></p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">n-th element of a JSON (sub-)array item counted from the end of the array (-1 is the last item).</p></td>\n</tr>\n</tbody>\n</table>\n<div class=\"paragraph\">\n<p>The following structure shows a minimalist example. We assume the data is stored in the variable \"JSONData\"</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-json\" data-lang=\"json\">{\n  \"UserInfo\": {\n    \"User\": \"Jane Doe\",\n    \"Location\": {\n      \"Country\": \"Germany\",\n      \"City\": \"Frankfurt\"\n    },\n    \"Pets\": [\n      {\n        \"Name\": \"Daisy\",\n        \"Type\": \"Cat\",\n        \"Age\": \"3\"\n      },\n      {\n        \"Name\": \"Duke\",\n        \"Type\": \"Dog\",\n        \"Age\": \"1\"\n      },\n      {\n        \"Name\": \"Felix\",\n        \"Type\": \"Cat\",\n        \"Age\": \"97\",\n        \"Color\": \"Black\"\n      }\n    ]\n  }\n}</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p><strong>Extract the value for \"Country\"</strong></p>\n</div>\n<div class=\"paragraph\">\n<p><code>${JSONData#UserInfo['Location']['Country']}</code> &#8594; \"Germany\"</p>\n</div>\n<div class=\"paragraph\">\n<p><strong>Extract a full entry from the \"Pets\" array</strong></p>\n</div>\n<div class=\"paragraph\">\n<p><code>${JSONData#UserInfo['Pets'][1]}</code> &#8594; \"{\"Name\":\"Duke\",\"Type\":\"Dog\",\"Age\":\"1\"}\"</p>\n</div>\n<div class=\"paragraph\">\n<p><strong>Extract the name for the second entry in the \"Pets\" array</strong></p>\n</div>\n<div class=\"paragraph\">\n<p><code>${JSONData#UserInfo['Pets'][1]['Name']}</code> &#8594; \"Duke\"</p>\n</div>\n<div class=\"admonitionblock note\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-note\" title=\"Note\"></i>\n</td>\n<td class=\"content\">\nYou can only extract single values or objects using this syntax, you can <strong>not</strong> extract arrays.\n</td>\n</tr>\n</table>\n</div>\n<div class=\"admonitionblock tip\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-tip\" title=\"Tip\"></i>\n</td>\n<td class=\"content\">\nIf you need to perform more complex operations on JSON structures, please refer to the <a href=\"/7.0/ki-syntax/functions/javascript/\">JavaScript</a> element.\n</td>\n</tr>\n</table>\n</div>\n</div>\n</div>","document":{"main":"5.VarStrings","title":"5.VarStrings","subtitle":""},"fields":{"toc":true,"location":["ki-syntax","definitions","varstrings"]}},"sidebarYaml":{"id":"6f28bdca-d703-5e79-ae00-b0508f8ee82a","showIndex":true}},"pageContext":{"id":"feba8521-e546-5153-8f0c-defe2efe073e","parent":"ki-syntax"}},"staticQueryHashes":["1010459453","1010459453","2356112386","2356112386","2603905930","2603905930","3026652197","3026652197","3167850324","3167850324","63159454","63159454"]}