json.path.put: Difference between revisions
Jump to navigation
Jump to search
m (Conversion script moved page json.path.put to Json.path.put without leaving a redirect: Converting page title to first-letter uppercase) |
No edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.path.put(json, path, key, value) | json.path.put(json, path, key, value) | ||
</ | </syntaxhighlight> | ||
'''Parameters''' | '''Parameters''' | ||
Line 19: | Line 19: | ||
|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> | ||
Which looks like this: | Which looks like this: | ||
< | <syntaxhighlight lang="javascript"> | ||
{ | { | ||
"Troll": { | "Troll": { | ||
Line 44: | Line 44: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
To add a new "AC" value to the troll, we could write | To add a new "AC" value to the troll, we could write | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h: monsters = json.path.put(monsters, "Troll", "AC", 12)] | [h: monsters = json.path.put(monsters, "Troll", "AC", 12)] | ||
<pre>[r: json.indent(json.get(monsters,"Troll"),2)]</pre> | <pre>[r: json.indent(json.get(monsters,"Troll"),2)]</pre> | ||
</ | </syntaxhighlight> | ||
And see this: | And see this: | ||
< | <syntaxhighlight lang="javascript"> | ||
{ | { | ||
"name": "Troll", | "name": "Troll", | ||
Line 62: | Line 62: | ||
"AC": 12 | "AC": 12 | ||
} | } | ||
</ | </syntaxhighlight> | ||
}} | }} | ||
[[Category:JSON Function]] | [[Category:JSON Function]] | ||
[[Category:JSON Path Function]] | [[Category:JSON Path Function]] |
Latest revision as of 23:59, 15 March 2023
json.path.put() Function
• Introduced in version 1.5.5
Create or update the key with the given value at the given path of a nested JSON Object. json.path.put() is only used creating/updating name/value pairs in JSON Objects. See json.path.set() for creating/updated values in arrays. 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)]
Which looks like this:
{
"Troll": {
"name": "Troll",
"HP": 75,
"Attacks": [
"Claw",
"Bite"
]
},
"Orc": {
"name": "Orc",
"HP": 13,
"Attacks": [
"Sword",
"Punch"
]
}
}
To add a new "AC" value to the troll, we could write
[h: monsters = json.path.put(monsters, "Troll", "AC", 12)]
<pre>[r: json.indent(json.get(monsters,"Troll"),2)]</pre>
And see this:
{
"name": "Troll",
"HP": 75,
"Attacks": [
"Claw",
"Bite"
]
"AC": 12
}