json.path.add: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 6: Line 6:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
json.path.add(json, path, value)
json.path.add(json, path, value)
</source>
</syntaxhighlight>


'''Parameters'''
'''Parameters'''
Line 18: Line 18:
|examples=
|examples=
Suppose we have the following nested json:
Suppose we have the following nested json:
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h:troll = json.set("{}", "name", "Troll", "HP", 75, "Attacks", json.append("Claw", "Bite"))]
[h:troll = json.set("{}", "name", "Troll", "HP", 75, "Attacks", json.append("Claw", "Bite"))]
[h:orc = json.set("{}", "name", "Orc", "HP", 13, "Attacks", json.append("Sword", "Punch"))]
[h:orc = json.set("{}", "name", "Orc", "HP", 13, "Attacks", json.append("Sword", "Punch"))]
[h:monsters = json.set("{}", "Troll", troll, "Orc", orc)]
[h:monsters = json.set("{}", "Troll", troll, "Orc", orc)]
</source>
</syntaxhighlight>


To add a new attack to our Orc, such as a "Bow" attack, we could type
To add a new attack to our Orc, such as a "Bow" attack, we could type


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[monsters = json.path.add(monsters, "Orc.Attacks", "Bow")]
[monsters = json.path.add(monsters, "Orc.Attacks", "Bow")]
</source>
</syntaxhighlight>


}}
}}

Latest revision as of 17:36, 15 March 2023

json.path.add() Function

Introduced in version 1.5.5
Add an element to a nested JSON Array. Additional information on how to specify the path is available here.

Usage

json.path.add(json, path, value)

Parameters

  • json - The json element in which the JSON Array is nested.
  • path - The path to the JSON Array.
  • value - The value to add to the array.

Examples

Suppose we have the following nested json:
[h:troll = json.set("{}", "name", "Troll", "HP", 75, "Attacks", json.append("Claw", "Bite"))]
[h:orc = json.set("{}", "name", "Orc", "HP", 13, "Attacks", json.append("Sword", "Punch"))]
[h:monsters = json.set("{}", "Troll", troll, "Orc", orc)]

To add a new attack to our Orc, such as a "Bow" attack, we could type

[monsters = json.path.add(monsters, "Orc.Attacks", "Bow")]