Generate Variables From JSON: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(New page: ==Generate Variables from JSON== When working with string properties, the {{func|varsFromStrProp}} function is an automated way to generate variables from the keys within a string pro...)
 
m (Taustin moved page generate Variables From JSON to Generate Variables From JSON without leaving a redirect)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
==Generate Variables from JSON==
When working with string properties, the {{func|varsFromStrProp}} function is an automated way to generate variables from the keys within a string property. For example, if you have the following string property:


When working with string properties, the {{func|[[varsFromStrProp]]}} function is an automated way to generate variables from the keys within a string property. For example, if you have the following string property:
<syntaxhighlight lang="mtmacro" line>
 
<source lang="mtmacro" line>
[prop = "name=Axe; damage=1d12; proficiency=2;"]
[prop = "name=Axe; damage=1d12; proficiency=2;"]
</source>
</syntaxhighlight>


You can use {{func|varsFromStrProp}} to generate a variable for each key - in other words, using the function to generate a list of locally accessible variables {{code|name}}, {{code|damage}}, and {{code|proficiency}}.  
You can use {{func|varsFromStrProp}} to generate a variable for each key - in other words, using the function to generate a list of locally accessible variables {{code|name}}, {{code|damage}}, and {{code|proficiency}}.  
Line 11: Line 9:
There is no equivalent function for JSON objects. However, the following routine can be inserted into any macro to efficiently generate the variables in question.
There is no equivalent function for JSON objects. However, the following routine can be inserted into any macro to efficiently generate the variables in question.


<source lang="mtmacro" line>
<hr>
<syntaxhighlight lang="mtmacro">
[h:varList=json.fields(testObj)]
[h:varList=json.fields(testObj)]


Line 19: Line 18:
     [set(var,value)]
     [set(var,value)]
}]
}]
</source>
</syntaxhighlight>
<hr>


The variable {{code|testObj}} represents the JSON object that is fed into the routine. The variables generated will be available within the scope of the running macro (but not outside).  
The variable {{code|testObj}} represents the JSON object that is fed into the routine. The variables generated will be available within the scope of the running macro (but not outside).  


[[Category:Cookbook]]
[[Category:Cookbook]]

Latest revision as of 18:31, 2 May 2023

When working with string properties, the varsFromStrProp() function is an automated way to generate variables from the keys within a string property. For example, if you have the following string property:

[prop = "name=Axe; damage=1d12; proficiency=2;"]

You can use varsFromStrProp() to generate a variable for each key - in other words, using the function to generate a list of locally accessible variables name, damage, and proficiency.

There is no equivalent function for JSON objects. However, the following routine can be inserted into any macro to efficiently generate the variables in question.


[h:varList=json.fields(testObj)]

[h,foreach(var,varList),CODE:
{
     [value = json.get(testObj,var)]
     [set(var,value)]
}]

The variable testObj represents the JSON object that is fed into the routine. The variables generated will be available within the scope of the running macro (but not outside).