Algorithm to merge two JSON

Viewed 44

I'm out of my wits trying to figure out how I can merge two JSON I have representing permissions into one. I have a default role that outlines the full extent of the hierarchy and when I merge it with other json, I need to pass on the parent assigned permissions to it's children if they were missing.

Default permission:

{
    "id": "default-role",
    "entities": [
        {
            "name": "entry",
            "permissions": [
                "read"
            ],
            "fields": [
                {
                    "name": "address",
                    "permissions": [
                        "read"
                    ],
                    "fields": [
                        {
                            "name": "zipcode",
                            "permissions": [
                                "read"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Custom Permissions:

{
    "id": "admin",
    "entities": [
        {
            "name": "entry",
            "permissions": [
                "read", "write"
            ]
        }
    ]
}

Merged expected output:

{
    "id": "defaultRole",
    "entities": [
        {
            "name": "entry",
            "permissions": [
                "read", "write"
            ],
            "fields": [
                {
                    "name": "address",
                    "permissions": [
                         "read", "write"
                    ],
                    "fields": [
                        {
                            "name": "zipcode",
                            "permissions": [
                                 "read", "write"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
0 Answers
Related