json.toVars: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by 3 users not shown)
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.  Characters in key values that are not allowed in MTScript variable names will be replaced with underscores.
Creates variables from a from a [[JSON Object]] or [[JSON Array]]. For 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.  For a JSON array, only the values are passed in and the variable names will be created from the 2nd parameter suffixed with a number.


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
json.toVars(jobj)
json.toVars(jobj)
json.toList(jobj, prefix)
json.toVars(jobj, prefix)
json.toList(jobj, prefix, suffix)
json.toVars(jobj, prefix, suffix)
</source>
json.toVars(jarr, varname)
</syntaxhighlight>


'''Parameters'''
'''Parameters'''
{{param|jobj|The json Object.}}
{{param|jobj|The JSON Object.}}
{{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 "".}}
{{param|jarr|A JSON array of values to be assigned to the created variables.}}
{{param|varname|A string with the base name for the variables to be created. Will be suffixed with the index in the array, i.e. myvar0, myvar1, myvar2, ....}}


'''Returns'''
'''Returns'''
Line 21: Line 24:


|examples=
|examples=
<source lang="mtmacro" line>
Basic use.
<syntaxhighlight lang="mtmacro" line>
[h: json.toVars(json.set("{}", "a", 1, "b", 2, "c", 3))]
[h: json.toVars(json.set("{}", "a", 1, "b", 2, "c", 3))]
[r: a] [r: b] [r: c]
[r: a] [r: b] [r: c]
</source>
</syntaxhighlight>
 
'''Output'''  
'''Output'''  
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
1 2 3</source>
1 2 3
</syntaxhighlight>


Example of a slightly more complex object.
Example of a slightly more complex object.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
<!-- get the inventory property that is a JSON object with  
<!-- get the inventory property that is a JSON object with  
     multiple JSON objects inside -->
     multiple JSON objects inside -->
Line 51: Line 57:
[r: v]: [r: eval(v)]<br>
[r: v]: [r: eval(v)]<br>
}]
}]
</source>
</syntaxhighlight>
 
'''Output'''
'''Output'''
<source lang="mtmacro">
<syntaxhighlight 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":{"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","backpack","belt"]
Line 65: Line 72:
torches_pack: 3
torches_pack: 3
rations_pack: 5
rations_pack: 5
</source>
</syntaxhighlight>
 
Using a JSON array as the first parameter.
<syntaxhighlight lang="mtmacro" line>
[h: myStats = json.rolls("3d6",6)]
[r: vars = json.toVars(myStats,"Stat")]<br>
[r, foreach(v,vars,""),code:{
[r: v]: [r: eval(v)]<br>
}]
</syntaxhighlight>
'''Output'''
<syntaxhighlight lang="mtmacro">
["Stat0","Stat1","Stat2","Stat3","Stat4","Stat5"]
Stat0: 10
Stat1: 14
Stat2: 9
Stat3: 8
Stat4: 11
Stat5: 13
</syntaxhighlight>
 
|changes=
{{change|1.5.7|Added {{code|JSON array}} parameter option.}}


}}
}}
[[Category:JSON Function]]
[[Category:JSON Function]]

Latest revision as of 17:39, 15 March 2023

json.toVars() Function

Introduced in version 1.5.5
Creates variables from a from a JSON Object or JSON Array. For 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. For a JSON array, only the values are passed in and the variable names will be created from the 2nd parameter suffixed with a number.

Usage

json.toVars(jobj)
json.toVars(jobj, prefix)
json.toVars(jobj, prefix, suffix)
json.toVars(jarr, varname)

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 "".
  • jarr - A JSON array of values to be assigned to the created variables.
  • varname - A string with the base name for the variables to be created. Will be suffixed with the index in the array, i.e. myvar0, myvar1, myvar2, ....

Returns JSON array of the created variable names.

Examples

Basic use.
[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

Using a JSON array as the first parameter.

[h: myStats = json.rolls("3d6",6)]
[r: vars = json.toVars(myStats,"Stat")]<br>
[r, foreach(v,vars,""),code:{
	[r: v]: [r: eval(v)]<br>
}]

Output

["Stat0","Stat1","Stat2","Stat3","Stat4","Stat5"]
Stat0: 10
Stat1: 14
Stat2: 9
Stat3: 8
Stat4: 11
Stat5: 13

Version Changes

  • 1.5.7 - Added JSON array parameter option.