Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to put two items of the dictionary into one entity in respective keys if idx=... is similar in keys then all the comparable data with matching keys becomes one item in dict with 0, 1, ... keys see below demo data to understand.

#### Demo Data:
```python
{
    "system": {
        "camera[idx=0]": {
            "fps": {
                "value": 24,
                "xpath": "/system/camera[idx=0]/fps",
                "index": 3,
                "string": "fps",
                "uniqueID": "f8f90bde-e530-4cc6-b350-3e923d6ab456",
                "editable": True,
                "parent": "system",
                "subParents": ["system", "camera[idx=0]"],
            },
        },
        "motion_detection[idx=0]": {
            "threshold_type": {
                "value": 0,
                "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=0]/threshold_type",
                "index": 5,
                "string": "threshold_type",
                "uniqueID": "5532aebe-501d-4275-ac4d-d6c8baf34d45",
                "editable": True,
                "parent": "system",
                "subParents": [
                    "system",
                    "camera[idx=1]",
                    "ip",
                    "motion_detection[idx=0]",
                ],
            },
        },
        "camera[idx=1]": {
            "vendor_name": {
                "value": "Raspberry",
                "xpath": "/system/camera[idx=1]/vendor_name",
                "index": 3,
                "string": "vendor_name",
                "uniqueID": "6ea8386b-fd11-44c4-88e8-b35b8eff9f43",
                "editable": True,
                "parent": "system",
                "subParents": ["system", "camera[idx=1]"],
            }
        },
        "motion_detection[idx=1]": {
            "threshold_min": {
                "value": 0,
                "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=1]/threshold_min",
                "index": 5,
                "string": "threshold_min",
                "uniqueID": "c8eab5a0-e00a-44e9-8320-09e4c8243505",
                "editable": True,
                "parent": "system",
                "subParents": [
                    "system",
                    "camera[idx=1]",
                    "ip",
                    "motion_detection[idx=1]",
                ],
            },
        },
    }
}
```
#### expecting data
```python
{
    "system": {
        "0": {
            "camera": {
                "fps": {
                    "value": 24,
                    "xpath": "/system/camera[idx=0]/fps",
                    "index": 3,
                    "string": "fps",
                    "uniqueID": "f8f90bde-e530-4cc6-b350-3e923d6ab456",
                    "editable": True,
                    "parent": "system",
                    "subParents": ["system", "camera[idx=0]"],
                },
            },
            "motion_detection": {
                "threshold_type": {
                    "value": 0,
                    "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=0]/threshold_type",
                    "index": 5,
                    "string": "threshold_type",
                    "uniqueID": "5532aebe-501d-4275-ac4d-d6c8baf34d45",
                    "editable": True,
                    "parent": "system",
                    "subParents": [
                        "system",
                        "camera[idx=1]",
                        "ip",
                        "motion_detection[idx=0]",
                    ]
                }
            }
        },
        "1": {
            "camera": {
                "vendor_name": {
                    "value": "Raspberry",
                    "xpath": "/system/camera[idx=1]/vendor_name",
                    "index": 3,
                    "string": "vendor_name",
                    "uniqueID": "6ea8386b-fd11-44c4-88e8-b35b8eff9f43",
                    "editable": True,
                    "parent": "system",
                    "subParents": ["system", "camera[idx=1]"]
                }
            },
            "motion_detection": {
                "threshold_min": {
                    "value": 0,
                    "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=1]/threshold_min",
                    "index": 5,
                    "string": "threshold_min",
                    "uniqueID": "c8eab5a0-e00a-44e9-8320-09e4c8243505",
                    "editable": True,
                    "parent": "system",
                    "subParents": [
                        "system",
                        "camera[idx=1]",
                        "ip",
                        "motion_detection[idx=1]",
                    ]
                }
            }
        }
    }
}
```


  [1]: https://favtutor.com/blogs/merge-dictionaries-python


What I have tried:

Learned This Page but I'm not able to do it if you know how to do it, please help me
Posted
Comments
Richard MacCutchan 7-Dec-22 4:27am    
I do not think there is any automatic way to do what you want. You will need to write some code to unpacke the various entries and rework them into the format that you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900