json.path.put: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 28: Line 28:


<source lang="mtmacro" line>
<source lang="mtmacro" line>
[r: json.path.put(monsters, "Troll", "AC", "12")]
[monsters = json.path.put(monsters, "Troll", "AC", "12")]
</source>
</source>


Line 34: Line 34:


[[Category:JSON Function]]
[[Category:JSON Function]]
[[Category:JSON Path Function]]

Revision as of 19:17, 19 September 2019

json.path.put() Function

Introduced in version 1.5.5
Add or update the key with the given value at the given path of a nested JSON Object. Additional information on how to specify the path is available here.

Usage

json.path.put(json, path, key, value)

Parameters

  • json - The json element in which the JSON Object is nested.
  • path - The path to the JSON Object.
  • key - The key of the JSON Object.
  • value - The value to associated with the key.

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 "AC" value to the troll, we could write

[monsters = json.path.put(monsters, "Troll", "AC", "12")]