json.set: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(New page: ==Function json.set== '''Introduced in MapTool 1.3b49''' Sets the value in at the specified index in a Macros:json array or for the specified key in a Macros:json object. You...)
 
No edit summary
 
(15 intermediate revisions by 8 users not shown)
Line 1: Line 1:
==Function json.set==
{{MacroFunction
|name=json.set
|version=1.3b49
|description=
Sets the value in at the specified index in a [[JSON Array]] or for the specified key in a [[JSON Object]]. You can use an empty string ({{code|""}}) to represent a new [[JSON Array]] or [[JSON Object]].


|usage=
<syntaxhighlight lang="mtmacro" line>
newarr=json.set(jarr, [])
</syntaxhighlight>
<syntaxhighlight lang="mtmacro" line>
newarr=json.set(jarr, index, value)
</syntaxhighlight>
<syntaxhighlight lang="mtmacro" line>
newarr=json.set(jarr, index, value, ..., ...)
</syntaxhighlight>
<syntaxhighlight lang="mtmacro" line>
newobj=json.set(jobj, key, value)
</syntaxhighlight>
<syntaxhighlight lang="mtmacro" line>
newobj=json.set(jobj, key, value, ..., ...)
</syntaxhighlight>
'''Parameters'''
{{param|jarr|The [[JSON Array]] that has an index's value set.}}
{{param|index|The numeric index which has its value set.}}
{{param|jobj|The [[JSON Object]] that has a key's value set.}}
{{param|key|The named key which has its value set.}}
{{param|value|The content that is set to the {{code|index}} or {{code|key}}.}}


'''Note:''' The {{code|index}} and {{code|value}} parameters or {{code|key}} and {{code|value}} parameters can be repeated in pairs.


'''Introduced in MapTool 1.3b49'''
'''Note:''' {{code|newarr}} or {{code|newobj}} can be equal to {{code|jarr}} or {{code|jobj}}. In this case, the object/array will simply be updated.


Sets the value in at the specified index in a [[Macros:json array|json array]] or for the specified key in a [[Macros:json object|json object]]. You can use an empty string ("") to represent a new json object.
'''Note:''' The word "class" is a reserved word in JavaScript; it is therefore not possible to create a JSON object containing a ''key'' named "Class," "class," or any mix of upper- and lower-case. MapTool will ignore any instructions to set a JSON object key with that name.


===Usage===
<source lang="mtmacro" line>
[h: jarr = json.set(jarr, index, value)]
[h: jarr = json.get(jobj, key, value)]
[h: jarr = json.get(jobj, key, value, ..., ...)]
</source>


 
|example=
===Examples===
<syntaxhighlight lang="mtmacro" line>
<source lang="mtmacro" line>
   [json.set("{}", "a", 5)]
   [json.set("{}", "a", 5)]
   [json.set("", "a", 5, "b", 8)]
   [json.set("", "a", 5, "b", 8)]
   [json.set(json.fromList("1,3"), 0, 8)]
   [json.set(json.fromList("1,3"), 0, 8)]
</source>
</syntaxhighlight>


Returns
Returns
Line 26: Line 46:
   {"a":5, "b":8}
   {"a":5, "b":8}
   [8, 3]
   [8, 3]
To save the new value in the [[JSON Array]] or [[JSON Object]] so that you can reference it later in a macro, you must set the array equal to the result of json.set().
<syntaxhighlight lang="mtmacro" line>
  [myArray = json.append("", 1, 2, 3)]
  [myArray = json.set(myArray, 0, 5)]
  [myArray]
</syntaxhighlight>
Returns
  [1,2,3]
  [5,2,3]
  [5,2,3]
|also=
{{func|json.get}}
}}
[[Category:JSON Function]]

Latest revision as of 16:41, 15 March 2023

json.set() Function

Introduced in version 1.3b49
Sets the value in at the specified index in a JSON Array or for the specified key in a JSON Object. You can use an empty string ("") to represent a new JSON Array or JSON Object.

Usage

newarr=json.set(jarr, [])
newarr=json.set(jarr, index, value)
newarr=json.set(jarr, index, value, ..., ...)
newobj=json.set(jobj, key, value)
newobj=json.set(jobj, key, value, ..., ...)

Parameters

  • jarr - The JSON Array that has an index's value set.
  • index - The numeric index which has its value set.
  • jobj - The JSON Object that has a key's value set.
  • key - The named key which has its value set.
  • value - The content that is set to the index or key.

Note: The index and value parameters or key and value parameters can be repeated in pairs.

Note: newarr or newobj can be equal to jarr or jobj. In this case, the object/array will simply be updated.

Note: The word "class" is a reserved word in JavaScript; it is therefore not possible to create a JSON object containing a key named "Class," "class," or any mix of upper- and lower-case. MapTool will ignore any instructions to set a JSON object key with that name.

Example

  [json.set("{}", "a", 5)]
  [json.set("", "a", 5, "b", 8)]
  [json.set(json.fromList("1,3"), 0, 8)]

Returns

 {"a":5}
 {"a":5, "b":8}
 [8, 3]

To save the new value in the JSON Array or JSON Object so that you can reference it later in a macro, you must set the array equal to the result of json.set().

   [myArray = json.append("", 1, 2, 3)]
   [myArray = json.set(myArray, 0, 5)]
   [myArray]

Returns

  [1,2,3] 
  [5,2,3] 
[5,2,3]

See Also