automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.75k stars 466 forks source link

How to merge objects in objects #335

Closed ConnysCode closed 3 years ago

ConnysCode commented 3 years ago

Hey, Hey, I'm trying to merge two JSON objects. But my problem now is that these objects also contain objects again and I want to join them as well, not overwrite them.

Here is an example:

My main JSON object

{
"users": {
"123": {
"active": true,
"password": "12345",
"settings": {
"some_setting": true
}
},
"456": {
"active": false,
"password": "67891",
"settings": {
"some_setting": false
}
}
}
}

My merging JSON Object

{
"users": {
"123": {
"password": "test",
"settings": {
"another_setting": "test"
}
}
}
}

I would like to get such an object as a result:

{
    "users": {
        "123": {
            "active": true,
            "password": "test",
            "settings": {
                "some_setting": true,
                "another_setting": "test"
            }
        },
        "456": {
            "active": false,
            "password": "67891",
            "settings": {
                "some_setting": false
            }
        }
    }
}

I hope you can help me! Kind regards, conny. 👋🏼

skokenes commented 3 years ago

This sounds like a JavaScript question, not an Automerge question? Perhaps try StackOverflow for that, this question has some options for you to consider: https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge

ConnysCode commented 3 years ago

Yes! Just thought, it might be possible with automerge. Thanks for the help tho 👍🏼