json.path.set

From RPTools Wiki
Revision as of 22:38, 5 September 2020 by Lwoestman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

json.path.set() Function

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

Usage

json.path.set(json, path, value)

Parameters

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

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 replace the Punch to an Axe, we can run

[monsters = json.path.set(monsters, "Orc.Attacks.[1]", "Axe")]

To change all "Sword" attacks to "Greatsword", we can run

[monsters = json.path.set(monsters, "*.Attacks[?(@ == 'Sword')]", "Great Sword")]