json.toVars: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{MacroFunction |name=json.toVars |version=1.5.5 |description= Creates variables from a from a JSON Object. The names of the variables are the keys of the object, while th...") |
No edit summary |
||
Line 3: | Line 3: | ||
|version=1.5.5 | |version=1.5.5 | ||
|description= | |description= | ||
Creates variables from a from a [[JSON Object]]. The names of the variables are the keys of the object, while the variables are assigned the values of the object. | Creates variables from a from a [[JSON Object]]. The names of the variables are the keys of the object, while the variables are assigned the values of the object. Characters in key values that are not allowed in MTScript variable names will be replaced with underscores. | ||
|usage= | |usage= | ||
Line 16: | Line 16: | ||
{{param|prefix|The prefix to add to the name of each variable. Defaults to an empty string "".}} | {{param|prefix|The prefix to add to the name of each variable. Defaults to an empty string "".}} | ||
{{param|suffix|The suffix to add to the name of each variable. Defaults to "".}} | {{param|suffix|The suffix to add to the name of each variable. Defaults to "".}} | ||
'''Returns''' | |||
JSON array of the created variable names. | |||
|examples= | |examples= | ||
Line 22: | Line 25: | ||
[r: a] [r: b] [r: c] | [r: a] [r: b] [r: c] | ||
</source> | </source> | ||
''' | '''Output''' | ||
<source lang="mtmacro"> | <source lang="mtmacro"> | ||
1 2 3</source> | 1 2 3</source> | ||
Example of a slightly more complex object. | |||
<source lang="mtmacro" line> | |||
<!-- get the inventory property that is a JSON object with | |||
multiple JSON objects inside --> | |||
[r: myInventory = getProperty("Inventory")]<br> | |||
<!-- Turn each key in the top level object to a variable and | |||
output the results. --> | |||
[r: vars = json.toVars(myInventory)]<br> | |||
[r, foreach(v,vars,""),code:{ | |||
[r: v]: [r: eval(v)]<br> | |||
}] | |||
<!-- Do the same for the belt object. Note that the variable | |||
belt was created by the preceding json.toVars() call --> | |||
[r: vars = json.toVars(belt,"b_")]<br> | |||
[r, foreach(v,vars,""),code:{ | |||
[r: v]: [r: eval(v)]<br> | |||
}] | |||
<!-- And again for the backpack object. --> | |||
[r: vars = json.toVars(backpack,"","_pack")]<br> | |||
[r, foreach(v,vars,""),code:{ | |||
[r: v]: [r: eval(v)]<br> | |||
}] | |||
</source> | |||
'''Output''' | |||
<source lang="mtmacro"> | |||
{"pockets":{"string":20,"candle":2,"picks":1},"backpack":{"bedroll":1,"torches":3,"rations":5},"belt":{"sword":1,"dagger":1,"healing potion":2,"flask of oil":1}} | |||
["pockets","backpack","belt"] | |||
pockets: {"string":20,"candle":2,"picks":1} | |||
backpack: {"bedroll":1,"torches":3,"rations":5} | |||
belt: {"sword":1,"dagger":1,"healing potion":2,"flask of oil":1} | |||
["b_sword","b_dagger","b_healing_potion","b_flask_of_oil"] | |||
b_sword: 1 b_dagger: 1 b_healing_potion: 2 b_flask_of_oil: 1 | |||
["bedroll_pack","torches_pack","rations_pack"] | |||
bedroll_pack: 1 | |||
torches_pack: 3 | |||
rations_pack: 5 | |||
</source> | |||
}} | }} | ||
[[Category:JSON Function]] | [[Category:JSON Function]] |
Revision as of 15:21, 22 September 2019
json.toVars() Function
• Introduced in version 1.5.5
Creates variables from a from a JSON Object. The names of the variables are the keys of the object, while the variables are assigned the values of the object. Characters in key values that are not allowed in MTScript variable names will be replaced with underscores.
Usage
json.toVars(jobj)
json.toList(jobj, prefix)
json.toList(jobj, prefix, suffix)
Parameters
jobj
- The json Object.prefix
- The prefix to add to the name of each variable. Defaults to an empty string "".suffix
- The suffix to add to the name of each variable. Defaults to "".
Returns JSON array of the created variable names.
Examples
[h: json.toVars(json.set("{}", "a", 1, "b", 2, "c", 3))]
[r: a] [r: b] [r: c]
Output
1 2 3
Example of a slightly more complex object.
<!-- get the inventory property that is a JSON object with
multiple JSON objects inside -->
[r: myInventory = getProperty("Inventory")]<br>
<!-- Turn each key in the top level object to a variable and
output the results. -->
[r: vars = json.toVars(myInventory)]<br>
[r, foreach(v,vars,""),code:{
[r: v]: [r: eval(v)]<br>
}]
<!-- Do the same for the belt object. Note that the variable
belt was created by the preceding json.toVars() call -->
[r: vars = json.toVars(belt,"b_")]<br>
[r, foreach(v,vars,""),code:{
[r: v]: [r: eval(v)]<br>
}]
<!-- And again for the backpack object. -->
[r: vars = json.toVars(backpack,"","_pack")]<br>
[r, foreach(v,vars,""),code:{
[r: v]: [r: eval(v)]<br>
}]
Output
{"pockets":{"string":20,"candle":2,"picks":1},"backpack":{"bedroll":1,"torches":3,"rations":5},"belt":{"sword":1,"dagger":1,"healing potion":2,"flask of oil":1}}
["pockets","backpack","belt"]
pockets: {"string":20,"candle":2,"picks":1}
backpack: {"bedroll":1,"torches":3,"rations":5}
belt: {"sword":1,"dagger":1,"healing potion":2,"flask of oil":1}
["b_sword","b_dagger","b_healing_potion","b_flask_of_oil"]
b_sword: 1 b_dagger: 1 b_healing_potion: 2 b_flask_of_oil: 1
["bedroll_pack","torches_pack","rations_pack"]
bedroll_pack: 1
torches_pack: 3
rations_pack: 5