json.path.set: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{MacroFunction |name=json.path.set |version=1.5.5 |description= Change an element in a nested JSON Array. Additional information on how to specify the path is available [...") |
No edit summary |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 6: | Line 6: | ||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.path. | json.path.set(json, path, value) | ||
</ | </syntaxhighlight> | ||
'''Parameters''' | '''Parameters''' | ||
Line 18: | Line 18: | ||
|examples= | |examples= | ||
Suppose we have the following nested json: | Suppose we have the following nested json: | ||
< | <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)] | ||
</ | </syntaxhighlight> | ||
To replace the Punch to an Axe, we can run | To replace the Punch to an Axe, we can run | ||
<syntaxhighlight lang="mtmacro" line> | |||
[monsters = json.path.set(monsters, "Orc.Attacks.[1]", "Axe")] | [monsters = json.path.set(monsters, "Orc.Attacks.[1]", "Axe")] | ||
</syntaxhighlight> | |||
To change all "Sword" attacks to "Greatsword", we can run | To change all "Sword" attacks to "Greatsword", we can run | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[monsters = json.path.set(monsters, "*.Attacks[?(@ == 'Sword')]", "Great Sword")] | [monsters = json.path.set(monsters, "*.Attacks[?(@ == 'Sword')]", "Great Sword")] | ||
</ | </syntaxhighlight> | ||
}} | }} | ||
[[Category:JSON Function]] | [[Category:JSON Function]] | ||
[[Category:JSON Path Function]] |
Latest revision as of 23:59, 15 March 2023
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")]