json.merge: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(New page: {{MacroFunction |name=json.merge |version=1.3b53 |description= Merges multiple JSON Arrays or JSON Objects. For JSON Arrays the value returned is that of all the JSON Array...)
 
No edit summary
 
(5 intermediate revisions by 4 users not shown)
Line 3: Line 3:
|version=1.3b53
|version=1.3b53
|description=
|description=
Merges multiple [[JSON Array]]s or [[JSON Object]]s. For [[JSON Array]]s the value returned is that of all the [[JSON Array]]s concatenated together. For [[JSON Object]]s the value returned is a [[JSON Object]] with all of the keys from all of the [[JSON Object]]s set, if eny key is specified in more
Merges multiple [[JSON Array]]s or [[JSON Object]]s.
than one [[JSON Object]] then the value for the last specified [[JSON Object]] is used.


|usage=
For [[JSON Array]]s the value returned is that of all the [[JSON Array]]s concatenated together.
<source lang="mtmacro" line>
[h: jarr = json.merge(jarr, jarr, ...)]
</source>


<source lang="mtmacro" line>
For [[JSON Object]]s the value returned is a [[JSON Object]] with all of the keys from all of the [[JSON Object]]s set, if any key is specified in more than one [[JSON Object]] then the value for the last specified [[JSON Object]] is used.
[h: jarr = json.merge(jobj, jobj, ...)]
</source>


|usage=
<syntaxhighlight lang="mtmacro" line>
json.merge(array, array, ...)
</syntaxhighlight>
<syntaxhighlight lang="mtmacro" line>
json.merge(object, object, ...)
</syntaxhighlight>
'''Parameters'''
'''Parameters'''
* {{code|jarr}} - A [[JSON Array]].
{{param|array|A [[JSON Array]].}}
* {{code|jobj}} - A [[JSON Object]].
{{param|object|A [[JSON Object]].}}
 
 


|examples=
|examples=
<source lang="mtmacro" line>
Merge three [[JSON Array]]s:
<syntaxhighlight lang="mtmacro" line>
[r: json.merge("[1,2]", "[3,4]", "[1,2]")]
[r: json.merge("[1,2]", "[3,4]", "[1,2]")]
</source>
</syntaxhighlight>
Returns: {{code|[1,2,3,4,1,2]}}


Returns
Merge two [[JSON Object]]s with no matching keys:
[1,2,3,4,1,2]
<syntaxhighlight lang="mtmacro" line>
 
<source lang="mtmacro" line>
[r: json.merge("{a:1, b:2}", "{c:10, d:7}")]
[r: json.merge("{a:1, b:2}", "{c:10, d:7}")]
</source>
</syntaxhighlight>
Returns: {{code|{"a":1,"b":2,"c":10,"d":7} }}


Returns
Merge three [[JSON Object]]s with a matching key, {{code|a}}:
{"a":1,"b":2,"c":10,"d":7}
<syntaxhighlight lang="mtmacro" line>
<source lang="mtmacro" line>
[r: json.merge("{a:1, b:2}", "{c:10, d:7}", "{a:11, z:7}")]
[r: json.merge("{a:1, b:2}", "{c:10, d:7}", "{a:11, z:7}")]
</source>
</syntaxhighlight>
 
Returns: {{code|{"a":11,"b":2,"c":10,"d":7,"z":7} }}
Returns
{"a":11,"b":2,"c":10,"d":7,"z":7}


|also=
{{func|json.union}}, {{func|json.intersection}}


|changes=
|changes=
{{change|1.3b54| fixed bug which allows {{code|json.merge()}} to work correctly with [[JSON Object]]s.}}
{{change|1.3b54|Fixed bug which allows {{code|json.merge()}} to work correctly with [[JSON Object]]s.}}


}}
}}
[[Category:JSON Function]]
[[Category:JSON Function]]

Latest revision as of 17:41, 15 March 2023

json.merge() Function

Introduced in version 1.3b53
Merges multiple JSON Arrays or JSON Objects.

For JSON Arrays the value returned is that of all the JSON Arrays concatenated together.

For JSON Objects the value returned is a JSON Object with all of the keys from all of the JSON Objects set, if any key is specified in more than one JSON Object then the value for the last specified JSON Object is used.

Usage

json.merge(array, array, ...)
json.merge(object, object, ...)

Parameters

Examples

Merge three JSON Arrays:
[r: json.merge("[1,2]", "[3,4]", "[1,2]")]

Returns: [1,2,3,4,1,2]

Merge two JSON Objects with no matching keys:

[r: json.merge("{a:1, b:2}", "{c:10, d:7}")]

Returns: {"a":1,"b":2,"c":10,"d":7}

Merge three JSON Objects with a matching key, a:

[r: json.merge("{a:1, b:2}", "{c:10, d:7}", "{a:11, z:7}")]
Returns: {"a":11,"b":2,"c":10,"d":7,"z":7}

See Also

Version Changes

  • 1.3b54 - Fixed bug which allows json.merge() to work correctly with JSON Objects.