json.path.delete: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Conversion script moved page Json.path.delete to json.path.delete: Converting page titles to lowercase)
No edit summary
 
Line 6: Line 6:


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


'''Parameters'''
'''Parameters'''
Line 16: Line 16:
|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 delete the "Punch" attack of the Orc, we could write
To delete the "Punch" attack of the Orc, we could write


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: json.path.delete(monsters, "Orc.Attacks.[1]")]
[r: json.path.delete(monsters, "Orc.Attacks.[1]")]
</source>
</syntaxhighlight>


or, alternatively,
or, alternatively,


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: json.path.delete(monsters, "Orc.Attacks[?(@ == 'Punch')]")]
[r: json.path.delete(monsters, "Orc.Attacks[?(@ == 'Punch')]")]
</source>
</syntaxhighlight>


Either statement return the json without the "Punch",
Either statement return the json without the "Punch",


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
{"Troll":{"name":"Troll","HP":75,"Attacks":["Claw","Bite"]},"Orc":{"name":"Orc","HP":13,"Attacks":["Sword"]}}
{"Troll":{"name":"Troll","HP":75,"Attacks":["Claw","Bite"]},"Orc":{"name":"Orc","HP":13,"Attacks":["Sword"]}}
</source>
</syntaxhighlight>


}}
}}

Latest revision as of 16:41, 15 March 2023

json.path.delete() Function

Introduced in version 1.5.5
Returns a JSON Array or JSON Object with deleted elements. These deletions are specified through the path parameter. Additional information on how to specify the path is availabe here.

Usage

json.path.delete(json, path)

Parameters

  • json - The json element to delete the value from.
  • path - The path of the values.

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 delete the "Punch" attack of the Orc, we could write

[r: json.path.delete(monsters, "Orc.Attacks.[1]")]

or, alternatively,

[r: json.path.delete(monsters, "Orc.Attacks[?(@ == 'Punch')]")]

Either statement return the json without the "Punch",

{"Troll":{"name":"Troll","HP":75,"Attacks":["Claw","Bite"]},"Orc":{"name":"Orc","HP":13,"Attacks":["Sword"]}}