Skip to content
Hawkmax edited this page Mar 15, 2026 · 3 revisions

Description

The $merge command combines the functionality of the $ref command and merges objects or arrays. ⚠️ Note: Arrays are merged differently compared to objects!

Syntax

mem: { "key": "$merge:<references>" }
key: no implementation

Param(s)

references: Define which objects or arrays are going to be joined , e.g.:
'path/to/obj1, path/to/obj2, ..., path/to/objn'.

Example

Before Apply

{ // <-- root
    "first_object": {
        "some_array": [ 0, 1, 2, 3 ],
        "some_string": "hi I am a string:)"
    },
    "second_object": {
        "some_array": [ 4 ],
        "some_string": "Hello World!",
        "some_number": 404
    },
    // In the line below, the `$merge` command will join the members `first_object` and `second_object`
    "join_first_second": "$merge:first_object, second_object",
    "join_second_first": "$merge:second_object, first_object",
    "join_some_arrays1": "$merge:first_object/some_array, second_object/some_array",
    "join_some_arrays2": "$merge:second_object/some_array, first_object/some_array"
}

After Apply

Note

When joining objects, the left-hand object loses against the right-hand object.
In other words:
left < right
"$merge:first_object, second_object"
This means that if both objects contain the same key, the values of the right object will overwrite those of the left.

{
    "first_object": {
        "some_array": [ 0, 1, 2, 3 ],
        "some_string": "hi I am a string:)"
    },
    "second_object": {
        "some_array": [ 4 ],
        "some_string": "Hello World!",
        "some_number": 404
    },

    "join_first_second": {                       // "$merge:first_object, second_object"
        "some_array": [ 4 ],
        "some_string": "Hello World!",
        "some_number": 404
    },
    "join_second_first": {                       // "$merge:second_object, first_object"
        "some_array": [ 0, 1, 2, 3 ],
        "some_string": "hi I am a string:)"
        "some_number": 404
    },

    "join_some_arrays1": [ 0, 1, 2, 3, 4 ],       // "$merge:first_object/some_array, second_object/some_array"
    "join_some_arrays2": [ 4, 0, 1, 2, 3 ]        // "$merge:second_object/some_array, first_object/some_array"
}

Author’s Recommendation: next read $merge_folder

Clone this wiki locally